Chapter 3 Algorithms
Transcript of Chapter 3 Algorithms
![Page 1: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/1.jpg)
PROG0101 Fundamentals of Programming
1
PROG0101
FUNDAMENTALS OF PROGRAMMING
Chapter 3
Algorithms
![Page 2: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/2.jpg)
PROG0101 Fundamentals of Programming
2
Algorithms
Introduction to Algorithms
• A sequence of instructions.
• A procedure or formula for solving a problem.
• It was created mathematician, Mohammed ibn-Musa
al-Khwarizmi.
• Often used for calculation, data processing and
programming.
• Algorithms can be expressed in any language.
![Page 3: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/3.jpg)
PROG0101 Fundamentals of Programming
3
Algorithms
Topics
• Algorithms
• Pseudocode
• Flowcharts
![Page 4: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/4.jpg)
PROG0101 Fundamentals of Programming
4
Algorithms
Introduction to Algorithms
• Algorithms for making things will often be divided into
sections;
– the parts/components/ingredients (inputs)
required to accomplish the task
– actions/steps/methods (processing) to produce
the required outcome (output).
• For example to build a model car, the parts (inputs)
are needed plus instructions on how to assemble the
car (processing) and the result is the car (output).
![Page 5: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/5.jpg)
PROG0101 Fundamentals of Programming
5
Algorithms
Introduction to Algorithms
• Two forms of Algorithm:
– Pseudocode
– Flow chart
![Page 6: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/6.jpg)
PROG0101 Fundamentals of Programming
6
Algorithms
Pseudocode
• Pseudocode (which means fake code, because its
not really programming code) specifies the steps
required to accomplish the task.
• Pseudocode is a type of structured English that is
used to specify an algorithm.
• Pseudocode cannot be compiled nor executed, and
there are no real formatting or syntax rules.
![Page 7: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/7.jpg)
PROG0101 Fundamentals of Programming
7
Algorithms
Pseudocode
Example of Pseudocode:
Ready and open subscriber file
Get a record
Do while more records
If current subscriber subscription count is > 3 then
Output the record
Get another record
end
![Page 8: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/8.jpg)
PROG0101 Fundamentals of Programming
8
Algorithms
Advantages of Pseudocode
• Reduced complexity.
• Increased flexibility.
• Ease of understanding.
![Page 9: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/9.jpg)
PROG0101 Fundamentals of Programming
9
Algorithms
Why is Pseudocode Necessary?
• The programming process is a complicated one.
• You must first understand the program specifications.
• Then you need to organize your thoughts and create
the program.
• You must break the main tasks that must be
accomplished into smaller ones in order to be able to
eventually write fully developed code.
• Writing Pseudocode will save you time later during
the construction & testing phase of a program's
development.
![Page 10: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/10.jpg)
PROG0101 Fundamentals of Programming
10
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
1. A computer can receive information
Read (information from a file)
Get (information from the keyboard)
2. A computer can put out information
Write (information to a file)
Display (information to the screen)
![Page 11: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/11.jpg)
PROG0101 Fundamentals of Programming
11
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
3. A computer can perform arithmetic
Use actual mathematical symbols or the words for
the symbols
Example:
Add number to total
Total = total + number
+, -, *, /
Calculate, Compute also used
![Page 12: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/12.jpg)
PROG0101 Fundamentals of Programming
12
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
4. A computer can assign a value to a piece of data
3 cases
i. to give data an initial value
Initialize, Set
![Page 13: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/13.jpg)
PROG0101 Fundamentals of Programming
13
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
4. A computer can assign a value to a piece of data
3 cases
ii. to assign a value as a result of some processing
„=‟
*x=5+y
![Page 14: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/14.jpg)
PROG0101 Fundamentals of Programming
14
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
4. A computer can assign a value to a piece of data
3 cases
iii. to keep a piece of information for later use
Save, Store
![Page 15: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/15.jpg)
PROG0101 Fundamentals of Programming
15
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
5. A computer can compare two piece of information
and select one of two alternative actions
IF condition THEN
some action
ELSE
alternative action
ENDIF
![Page 16: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/16.jpg)
PROG0101 Fundamentals of Programming
16
Algorithms
How to Write Pseudocode Statements?
There are six basic computer operations
6. A computer can repeat a group of actions
WHILE condition (is true)
some action
ENDWHILE
FOR a number of times
some action
ENDFOR
![Page 17: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/17.jpg)
PROG0101 Fundamentals of Programming
17
Algorithms
Data Dictionaries
• The pseudo code by itself doesn't provide enough
information to be able to write program code.
• Data Dictionaries are used to describe the data used
in the Pseudo Code.
• The standard data types used in Pseudo Code are
Integer, Double, String, Char and Boolean.
![Page 18: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/18.jpg)
PROG0101 Fundamentals of Programming
18
Algorithms
Data Dictionaries
Name Data Type Description
number1 Integer The first number to
be added
number2 Integer The second number
to be added
total Integer The total of
number1 and
number2 added
together
![Page 19: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/19.jpg)
PROG0101 Fundamentals of Programming
19
Algorithms
Example 1:
Program Specification:
Pseudocode:
Write a program that obtains two integer numbers from the
user. It will print out the sum of those numbers.
Prompt the user to enter the first integer
Prompt the user to enter a second integer
Compute the sum of the two user inputs
Display an output prompt that explains the answer as the sum
Display the result
![Page 20: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/20.jpg)
PROG0101 Fundamentals of Programming
20
Algorithms
Example 2:
Finding average of any three numbers.
We might usually specify the procedure of solving this
problem as “add the three numbers and divide by
three”. Here, Read (or Ask) and Write (or Say) are
implied. However in an algorithm, these steps have to
be made explicit. Thus a possible algorithm is:
![Page 21: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/21.jpg)
PROG0101 Fundamentals of Programming
21
Algorithms
Example 2:
Step 1 Start
Step 2 Read values of X, Y, Z
Step 3 S = X + Y + Z
Step 4 A = S / 3
Step 5 Write value of A
Step 6 Stop
![Page 22: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/22.jpg)
PROG0101 Fundamentals of Programming
22
Algorithms
Example 2:
Or you can write like this:
Step 1 Start
Step 2 Read values of X, Y, Z
Step 3 S = X + Y + Z
Step 4 A = S / 3
Step 5 Write value of A
Step 6 Stop
![Page 23: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/23.jpg)
PROG0101 Fundamentals of Programming
23
Algorithms
Example 3:
Finding square and cube.
Step 1 Start
Step 2 Read value of N
Step 3 S = N * N
Step 4 C = S * N
Step 5 Write values of S, C
Step 6 Stop
![Page 24: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/24.jpg)
PROG0101 Fundamentals of Programming
24
Algorithms
Example 4:
Finding biggest of two numbers.
Step 1 Start
Step 2 Read A, B
Step 3 If A > B, then BIG = A, otherwise BIG = B
Step 4 Write BIG
Step 5 Stop
![Page 25: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/25.jpg)
PROG0101 Fundamentals of Programming
25
Algorithms
Example 5:
Calculate pay.
Step 1 Start
Step 2 Input hours
Step 3 Input rate
Step 4 pay = hours * rate
Step 5 Print pay
Step 6 End
![Page 26: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/26.jpg)
PROG0101 Fundamentals of Programming
26
Algorithms
Exercise:
Write a Pseudocode for these problems.
1. S = (A + B + C) / Y
2. Convert from Celsius to Fahrenheit.
3. Area of Circle (r2)
4. Volume of Sphere ( r3)
5. Average speed =
4
3
Distance traveled
Time taken
(Multiply by 9, then divide by 5, then add 32 )
![Page 27: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/27.jpg)
PROG0101 Fundamentals of Programming
27
Algorithms
Flowchart
• A flowchart is a graphical representation of an
algorithm.
• These flowcharts play a vital role in the programming
of a problem and are quite helpful in understanding
the logic of complicated and lengthy problems.
• Once the flowchart is drawn, it becomes easy to
write the program in any high level language.
![Page 28: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/28.jpg)
PROG0101 Fundamentals of Programming
28
Algorithms
Flowchart
• A flowchart can therefore be used to:
– Define and analyze processes
– Build a step-by-step picture of the process for
analysis, discussion, or communication
– Define, standardize or find areas for
improvement in a process
![Page 29: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/29.jpg)
PROG0101 Fundamentals of Programming
29
Algorithms
Flowchart
![Page 30: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/30.jpg)
PROG0101 Fundamentals of Programming
30
Algorithms
Flowchart Symbols
Start and end symbols
• Represented as lozenges, ovals or rounded
rectangles
• Usually containing the word "Start" or "End", or
another phrase signalling the start or end of a
process, such as "submit enquiry" or "receive
product".
![Page 31: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/31.jpg)
PROG0101 Fundamentals of Programming
31
Algorithms
Flowchart Symbols
Arrows
• Showing what's called "flow of control" in computer
science.
• An arrow coming from one symbol and ending at
another symbol.
• Represents that control passes to the symbol the
arrow points to.
![Page 32: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/32.jpg)
PROG0101 Fundamentals of Programming
32
Algorithms
Flowchart Symbols
Processing steps
• Represented as rectangles.
• Examples: “Add 1 to X"; "replace identified part";
"save changes" or similar.
![Page 33: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/33.jpg)
PROG0101 Fundamentals of Programming
33
Algorithms
Flowchart Symbols
Input/Output
• Represented as a parallelogram.
• Examples: Get X from the user; display X.
![Page 34: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/34.jpg)
PROG0101 Fundamentals of Programming
34
Algorithms
Flowchart Symbols
Conditional or decision
• Represented as a diamond (rhombus).
• These typically contain a Yes/No question or
True/False test.
![Page 35: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/35.jpg)
PROG0101 Fundamentals of Programming
35
Algorithms
Flowchart Symbols
Display
• Indicates a process flow step where information is
displayed to a person (e.g., PC user, machine
operator).
![Page 36: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/36.jpg)
PROG0101 Fundamentals of Programming
36
Algorithms
Rules for Flowchart
1. Every flow chart has a START symbol and a STOP
symbol.
2. The flow of sequence is generally from the top of the
page to the bottom of the page. This can vary with
loops which need to flow back to an entry point.
3. Use arrow-heads on connectors where flow direction
may not be obvious.
4. There is only one flow chart per page.
![Page 37: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/37.jpg)
PROG0101 Fundamentals of Programming
37
Algorithms
Rules for Flowchart
5. A page should have a page number and a title.
6. A flow chart on one page should not break and jump
to another page
7. A flow chart should have no more than around 15
symbols (not including START and STOP).
![Page 38: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/38.jpg)
PROG0101 Fundamentals of Programming
38
Algorithms
Advantages of Using Flowcharts
• Communication: Flowcharts are better way of
communicating the logic of a system to all concerned.
• Effective analysis: With the help of flowchart, problem
can be analysed in more effective way.
• Proper documentation: Program flowcharts serve as a
good program documentation, which is needed for various
purposes.
• Efficient Coding: The flowcharts act as a guide or
blueprint during the systems analysis and program
development phase.
![Page 39: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/39.jpg)
PROG0101 Fundamentals of Programming
39
Algorithms
Advantages of Using Flowcharts
• Proper Debugging: The flowchart helps in debugging
process.
• Efficient Program Maintenance: The maintenance of
operating program becomes easy with the help of
flowchart. It helps the programmer to put efforts more
efficiently on that part.
![Page 40: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/40.jpg)
PROG0101 Fundamentals of Programming
40
Algorithms
Basic Control Structures
• Sequence
• Selection
• Loop
![Page 41: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/41.jpg)
PROG0101 Fundamentals of Programming
41
Algorithms
Basic Control Structures
Sequence
• Steps that execute in sequence are represented by symbols
that follow each other top to bottom or left to right.
• Top to bottom is the standard.
![Page 42: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/42.jpg)
PROG0101 Fundamentals of Programming
42
Algorithms
Basic Control Structures
Sequence
![Page 43: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/43.jpg)
PROG0101 Fundamentals of Programming
43
Algorithms
Basic Control Structures
Selection
• Once the condition is evaluated, the control flows into one of
two paths.
• Once the conditional execution is finished, the flows rejoin
before leaving the structure.
![Page 44: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/44.jpg)
PROG0101 Fundamentals of Programming
44
Algorithms
Basic Control Structures
Selection
![Page 45: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/45.jpg)
PROG0101 Fundamentals of Programming
45
Algorithms
Basic Control Structures
Loop
• Either the processing repeats or the control leaves
the structure.
• Notice that the return line joins the entry line before
the question.
![Page 46: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/46.jpg)
PROG0101 Fundamentals of Programming
46
Algorithms
Basic Control Structures
Loop
![Page 47: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/47.jpg)
PROG0101 Fundamentals of Programming
47
Algorithms
Example 1:
Algorithm:
Input: two numbers x and y
Output: the average of x and y
Steps:
1. input x
2. input y
3. sum = x + y
4. average = sum /2
5. output average
![Page 48: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/48.jpg)
PROG0101 Fundamentals of Programming
48
Algorithms
Example 1:
![Page 49: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/49.jpg)
PROG0101 Fundamentals of Programming
49
Algorithms
Example 2:
Draw a flowchart to find the largest of three numbers A,
B, and C.
![Page 50: Chapter 3 Algorithms](https://reader031.fdocuments.in/reader031/viewer/2022020113/58668b8c1a28abe33f8b72b4/html5/thumbnails/50.jpg)
PROG0101 Fundamentals of Programming
50
Algorithms
Example 2: