FLOWCHART
ENGG 0304
WHAT IS A FLOWCHART?
START
Display message
“How many
hours did you
work?”
• A flowchart is a diagram that Read Hours
depicts the “flow” of a Display message
program. “How much do
you get paid per
hour?”
Read PayRate
• The figure shown here is a Multiply Hours
flowchart for the pay-
by PayRate.
Store result in
GrossPay.
calculating program shown Display
in Program 1-1.
GrossPay
END
BASIC SYMBOLS START
Display message
Terminal
“How many
hours did you
work?”
• Terminals Read Hours
• represented by rounded Display message
rectangles “How much do
you get paid per
hour?”
• indicate a starting or
ending point
Read PayRate
Multiply Hours
by PayRate.
START Store result in
GrossPay.
Display
GrossPay
END Terminal
END
BASIC SYMBOLS START
Display message
“How many
hours did you
work?”
• Input/Output Operations Read Hours
• represented by Display message
parallelograms “How much do
you get paid per
Input/Output
hour?” Operation
• indicate an input or output
operation
Read PayRate
Multiply Hours
by PayRate.
Display message Store result in
GrossPay.
“How many
Read Hours
hours did you Display
GrossPay
work?”
END
BASIC SYMBOLS START
Display message
“How many
hours did you
work?”
• Processes Read Hours
• represented by rectangles Display message
• indicates a process such “How much do
you get paid per
hour?”
as a mathematical
computation or variable
Read PayRate
assignment Multiply Hours
by PayRate.
Store result in
Process GrossPay.
Multiply Hours
by PayRate. Display
Store result in GrossPay
GrossPay.
END
FOUR FLOWCHART
STRUCTURES
• Sequence
• Decision
• Repetition
• Case
SEQUENCE STRUCTURE
• A series of actions are performed in
sequence
• The pay-calculating example was a
sequence flowchart.
DECISION STRUCTURE
• The flowchart segment below shows how
a decision structure is expressed in C++
as an if/else statement.
Flowchart C++ Code
NO YES if (x < y)
x < y? a = x * 2;
else
Calculate a Calculate a a = x + y;
as x plus y. as x times 2.
DECISION STRUCTURE
• The flowchart segment below shows a
decision structure with only one action to
perform. It is expressed as an if
statement Flowchart
in C++ code.
C++ Code
NO YES
x < y? if (x < y)
a = x * 2;
Calculate a
as x times 2.
REPETITION STRUCTURE
• The flowchart segment below shows a
repetition structure expressed in C++ as
a while loop.
Flowchart C++ Code
while (x < y)
YES x++;
x < y? Add 1 to x
CONTROLLING A REPETITION
STRUCTURE
• The action performed by a repetition structure
must eventually cause the loop to terminate.
Otherwise, an infinite loop is created.
• In this flowchart segment, x is never changed.
Once the loop starts, it will never end.
• QUESTION: How can this
flowchart be modified so YES
x < y?
it is no longer an infinite Display x
loop?
CONTROLLING A REPETITION
STRUCTURE
• ANSWER: By adding an action within the
repetition that changes the value of x.
YES
x < y? Display x Add 1 to x
CASE STRUCTURE
If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
If years_employed = 1, CASE If years_employed is any
bonus is set to 100 years_employed other value, bonus is set
to 800
1 2 3 Other
bonus = 100 bonus = 200 bonus = 400 bonus = 800
CONNECTORS
• The “A” connector indicates that the
second flowchart segment begins where
the first segment ends. START A
END
A
MODULES
START
• The position of the Read Input.
module symbol indicates
the point the module is
executed. Call calc_pay
function.
• A separate flowchart can
be constructed for the Display results.
module.
END
COMBINING STRUCTURE
• This flowchart segment
shows two decision NO x > min?
YES
structures combined.
Display “ x is
outside the limits”
NO YES
x < max?
Display “ x is Display “ x is
outside the limits” within limits”
ANSWER
• What do each of the following symbols
represent?
Decision
Terminal
Input/Output
Operation Connector
Module
Process
REVIEW
• Name the four flowchart structures.
ANSWER
• Sequence
• Decision
• Repetition
• Case
REVIEW
• What type of structure is this?
ANSWER
• Repetition
REVIEW
• What type of structure is this?
ANSWER
• Sequence
REVIEW
• What type of structure is this?
ANSWER
• Case
REVIEW
• What type of structure is this?
ANSWER
• Decision
TRY: CLASS AVERAGE
ALGORITHM
• Problem: Calculate and report the grade-point
average for a class
• Discussion: The average grade equals the sum
of all grades divided by the number of students
• Output: Average grade
• Input: Student grades
• Processing: Find the sum of the grades; count
the number of students; calculate average
START
START
Initialize
counter and
sum to 0
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Increment
counter
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Increment
counter
Add grade to
sum
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Increment
counter
Add grade to
sum
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Display Increment
average counter
Add grade to
sum
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Display Increment
average counter
END Add grade to
sum