Flow Chart
Flow Chart
CONCEPT
Figure 8-1
Informal definition of an algorithm
used in a computer
Figure 8-2
Finding the largest integer
among five integers
Figure 8-3
Defining actions in FindLargest algorithm
Figure 8-4
FindLargest refined
Figure 8-5
Generalization of FindLargest
8.2
THREE CONSTRUCTS
Figure 8-6
Three constructs
8.3
ALGORITHM
REPRESENTATION
Figure 8-7
Flowcharts for three constructs
Figure 8-8
Pseudocode for three constructs
Example 1
Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 4
Solution
See Algorithm 8.5 on the next slide.
Algorithm 8.5:Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
8.5
SUBALGORITHMS
Figure 8-9
Concept of a subalgorithm
Algorithm 8.6: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLarger
End while
3. Return Largest
End
Subalgorithm: Find larger
FindLarger
Input: Largest and current integer
1. if (the integer is greater than Largest)
then
1.1 Set Largest to the value of the integer
End if
End
8.6
BASIC
ALGORITHMS
Figure 8-10
Summation
Figure 8-11
Product
Figure 8-12
Selection sort
Figure 8-13: part I
Example of selection sort
Figure 8-13: part II
Example of selection sort
Figure 8-14
Selection sort
algorithm
Flowchart:
What is a Flowchart?
The flowchart is a means of visually presenting the flow
of control through an information processing systems,
the operations performed within the system and the
sequence in which they are performed.
It is a graphic representation of how a process works,
showing, at a minimum, the sequence of steps.
Flowcharts are generally drawn in the early stages of
formulating computer solutions.
Flowchart (Contd…):
Guideline for drawing a flowchart:
Flowcharts are usually drawn using some standard
symbols; Some standard symbols, which are frequently
required for flowcharting many computer programs are
shown below
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.
Principles of Programming - NI
July 2005 32
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.
Principles of Programming - NI
July 2005 33
Flowchart (Contd…):
A set of useful standard Flowchart
symbols:
Rounded box
use it to represent an event which occurs automatically.
Rectangle or box
use it to represent an event which is controlled within
the process. Typically this will be a step or action which
is taken.
Diamond
use it to represent a decision point in the process.
Circle
use it to represent a point at which the flowchart
connects with another process.
ADVANTAGES OF USING
FLOWCHARTS:
Communication: Flowcharts are better way of
communicating the logic of a system
Effective analysis: Problem can be analyzed in more
effective way.
Proper documentation: Flowcharts serve as a good
program documentation
Efficient Coding: Flowcharts act as a guide or blueprint
during the systems analysis and program development
phase.
ADVANTAGES OF USING
FLOWCHARTS (Contd…):
Proper Debugging: Flowchart helps in debugging
process.
Start
Read A, B
Yes No
Is A > B
Print A Print B
End
Flowchart to find the largest of
three numbers A,B, and C:
NO
LIMITATIONS OF USING
FLOWCHARTS:
Complex logic: Sometimes, the program logic is quite
complicated. In that case, flowchart becomes complex
and clumsy.
Pseudocode:
Input a set of 4 marks
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
example
Design with
Structure Charts
Design Process
4 3
Write code to
Improve readabililty implement the
of the code by top-down design.
incorporating As you mature this
functions. will be skipped by
going to step 4.
National Parcel Example
Example 2 : Parcel Service Company.
Emphasizes IF notation for top down design.
National Parcel Service (NPS) specializes in nationwide delivery of small packages. NPS will not accept
any packages whose largest dimension is greater than 3 feet or whose weight exceeds 50 pounds.
The charge for shipping a parcel is $0.75 plus an amount based on package weight as follows:
Weight (lb.) Rate
-----------------------------
20 or less $0.08 per lb.
40 or less $0.10 per lb.
Over 40 $0.15 per lb.
-----------------------------
There is an additional $1.00 charge if the volume of the package exceeds 18 cubic feet. Write a program
that will read the dimensions of a parcel (in feet) and its weight (in pounds) and then compute and print
the postage due. If the package is rejected, an appropriate message should be printed.
Parcel *
Charge
Parcel
Charge
Compute Postage
Charge=
Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18 Wt_Chrg+
Vol_Chrg+
0.75
Wt_chrg= Wt_chrg= Wt_chrg= Vol_chrg= Vol_chrg=
Wt*0.08 Wt*0.1 Wt*0.15 0.0 1.0