Lab 5 - Flow Chart and PseudoCode
Lab 5 - Flow Chart and PseudoCode
1
Agenda
• Flow Chart
• Pseudo-code
• Exercises
2
What is a Flowchart?
START
Display message
“How many
hours did you
work?”
of a program.
Read PayRate
• The figure shown
here is a flowchart Multiply Hours
by PayRate. Store
calculating. Display
GrossPay
END
3
Basic Flowchart START Terminal
rounded rectangles
“How much do
you get paid per
hour?”
o indicate a starting
or ending point Read PayRate
Multiply Hours
by PayRate. Store
START result in
GrossPay.
Display
GrossPay
END Terminal
END
4
Basic Flowchart START
5
Basic Flowchart START
o represented by
rectangles Display message
“How much do
you get paid per
o indicates a process hour?”
variable assignment
by Pay Rate.
Process Store result in
GrossPay.
Multiply Hours
by PayRate. Display
GrossPay
Store result in
GrossPay. END
6
Stepping Through START
Output
Read Hours
How many
hours did
you work?
Display message
“How much do
you get paid per
hour?”
Read PayRate
Multiply Hours
by PayRate. Store
result in
Variable Contents: GrossPay.
Hours: ?
PayRate: ? Display
GrossPay
GrossPay: ?
END
7
Stepping Through START
How many
Input Read Hours
hours did Operation
you work?
(User types 40) Display message
40
“How much do
you get paid per
hour?”
Read PayRate
Multiply Hours
by PayRate. Store
result in
GrossPay.
Variable Contents:
Hours: 40
Display
PayRate: ? GrossPay
GrossPay: ?
END
8
Stepping Through START
Read Hours
How much
do you get
paid per
Display message
hour?
“How much do
Output you get paid per
Operation hour?”
Read PayRate
Multiply Hours
by PayRate. Store
result in
GrossPay.
Variable Contents:
Hours: 40 Display
PayRate: ? GrossPay
GrossPay: ?
END
9
Stepping Through START
Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
you get paid per
hour?”
Hours: 40
PayRate: 20 Display
GrossPay
GrossPay: ?
END
10
Stepping Through START
Read Hours
How much
do you get
paid per
Display message
hour?
“How much do
you get paid per
hour?”
Read PayRate
Multiply Hours
Process: The by PayRate. Store
result in
product of
Variable Contents: 40 times 20 is
GrossPay.
Hours: 40 stored in
PayRate: 20 Gross Pay Display
GrossPay
GrossPay: 800
END
11
Stepping Through START
Read Hours
Your gross
pay is 800
Display message
“How much do
you get paid per
hour?”
Read PayRate
Multiply Hours
by PayRate. Store
result in
Variable Contents: GrossPay.
Hours: 40
PayRate: 20 Output Display
Operation GrossPay
GrossPay: 800
END
12
Four Flowchart Structures
• Sequence
• Decision
• Case
• Repetition
13
Sequence Structure
• A series of actions are performed in
sequence
• The pay-calculating example was a
sequence flowchart.
14
Decision Structure
• One of two possible actions is taken,
depending on a condition.
NO YES
15
Decision Structure
• A new symbol, the diamond, indicates a yes/no
question. If the answer to the question is yes, the
flow follows one path. If the answer is no, the flow
follows another path
NO YES
16
Decision Structure
• In the flowchart segment below, the question “is x <
y?” is asked. If the answer is no, then process A is
performed. If the answer is yes, then process B is
performed.
NO YES
x < y?
Process A Process B
17
Decision Structure
• The flowchart segment below shows how a decision
structure that multiplies x by 2 if x is less than y.
otherwise, it adds x to y
NO YES
x < y?
Calculate a Calculate a
as x plus y. as x times 2.
18
Case Structure
• One of several possible actions is taken,
depending on the contents of a variable.
19
Case Structure
• The structure below indicates actions to
perform depending on the value in
years_employed.
CASE
years_employed
=1 =2 =3 Otherwise
20
Case Structure
If years_employed = If years_employed =
2, bonus is set to 200 3, bonus is set to 400
If years_employed = If years_employed is
CASE
1, bonus is set to 100 years_employed any other value,
bonus is set to 800
=1 =2 =3 Otherwise
21
Repetition Structure
• A repetition structure represents part of the program
that repeats. This type of structure is commonly
known as a loop.
22
Repetition Structure
• Notice the use of the diamond symbol. A loop tests
a condition, and if the condition exists, it performs
an action. Then it tests the condition again. If the
condition still exists, the action is repeated. This
continues until the condition no longer exists.
23
Repetition Structure
• In the flowchart segment, the question “is x < y?” is
asked. If the answer is yes, then Process A is
performed. The question “is x < y?” is asked again.
Process A is repeated as long as x is less than y.
When x is no longer less than y, the repetition stops
and the structure is exited.
YES
x < y? Process A
24
Repetition Structure
• The flowchart segment below shows a repetition
structure that increments x by 1 as long as x is less
than y.
YES
x < y? Add 1 to x
25
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
YES
end. Display x
x < y?
• QUESTION: How can this
flowchart be modified so
it is no longer an infinite
loop?
26
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
27
A Pre-Test Repetition
Structure
• This type of structure is known as a pre-test
repetition structure. The condition is tested BEFORE
any actions are performed.
YES
x < y? Display x Add 1 to x
28
A Pre-Test Repetition
Structure
• In a pre-test repetition structure, if the condition
does not exist, the loop will never begin.
YES
x < y? Display x Add 1 to x
29
A Post-Test Repetition
Structure
• This flowchart segment
shows a post-test
repetition structure.
• The condition is tested
Display x
AFTER the actions
are performed.
• A post-test repetition Add 1 to x
structure always
performs its actions at
YES
least once. x < y?
30
WHILE Loop (pre-test) Expression is
evaluated at the
start of each
iteration of the loop
Expression
If expression is false
true ,Action is
executed true
If expression is
Action false, program
execution
continues with
next statement
outside the loop
31
DO WHILE Loop(post-test)
Action
Action
Expressio true
Expression
n
False
If condition is true the Condition
Action is executed
True
33
WHILE Loop
Start
count no
<10
yes
Write
add 1 to “The End”
count
Stop
write count
34
Start
DO WHILE Loop
count = 0
Program that increments an
integer by one and displays the
new value as long as it is less
than 10.
add 1 to
count
write count
yes count
<10
no
Write
“The End”
Stop 35
FOR Loop
Start
count no
<10
yes
Write
“The End”
write count
add 1 to Stop
count
36
Connectors
•The “A” connector
indicates that the START A
second flowchart
segment begins
where the first
segment ends.
END
A
37
Subroutines (Modules)
•A program module (such as a
START
module is executed.
Display
•A separate flowchart can be results.
38
Combining Structures
• Structures are commonly combined to create more
complex algorithms.
• The flowchart segment below combines a decision
structure with a sequence structure.
YES
x < y? Display x Add 1 to x
39
Combining Structures
• This flowchart
segment
shows two NO YES
decision x > min?
structures
combined. Display “x is
outside the
NO YES
limits.” x < max?
40
Pseudo-code
41
Rules for Pseudo-code
• Write only one statement per line
42
Rules for Variable Names
• Begin with lowercase letter
• Contain no spaces
• Additional words begin with capital
• Unique names within code
• Consistent use of names
43
Advantages & Disadvantages
Flowchart Advantages: Pseudocode
Standardized Advantages
Visual Easily modified
Done easily on Word
Processor
Flowchart Pseudocode
Disadvantages: Disadvantages:
Hard to modify Not visual
Special software required No accepted standard, varies
from company to company
44
Flow chart review
• What do each of the following symbols
represent?
Decision
Terminal
Input/ Output
Operation Connector
Process Module
45
Review
• What type of structure is this?
Repetition
46
Review
• What type of structure is this?
Sequence
Case
Decision
50
Logical Operators
NOT (!)
AND (&&)
OR (||)
51
Logical Operators: Examples
IF (x = 32) AND (y = 7) THEN
sumXY = x + y
52
Exercises
53
Exercise 1
• Algorithm for program that reads a number and
determines whether it is positive, negative or zero.
54
Exercise 1: Solution
START
Input
number BEGIN
READ number
Number True IF number > 0 THEN
>0 PRINT “Positive Number”
Print
False “Positive Number” ELSE IF Number < 0 THEN
PRINT “Negative Number”
number True
<0
Print ELSE
False “Negative Number” PRINT ”Zero”
Print ENDIF
“Zero” END
END 55
Exercise 2
• Algorithm for program that checks whether an
input number lies within a specified range.
For example: If user enters range (20, 50) and
queried number 34, the program should display “In
range”.
On the other hand, if he enters 76, the program
should display “Not in Range”.
56
Exercise 2: Solution
• Using nested IF structure
START
Input
startRange, endRange,
number
number True
False
>=
startRange
False number
<=
endRange
Print
”Not in Range”
True
Print
”In Range”
END 57
Exercise 2: Solution
BEGIN
READ startRange, endRange, number
58
Exercise 2: Another Solution
• Using logical operators in IF condition
START
Input
startRange, endRange,
number
END 59
Exercise 2: Another Solution
BEGIN
READ startRange, endRange, number
ELSE
PRINT “Not in Range”
ENDIF
END
60
Exercise 3
• Algorithm for program that determines whether a
baby’s weight is normal or not.
For girls, normal babies weight are 2.5 to 4.5 KG.
On the other hand, for boys the normal weights are
4 to 5.5 KG.
61
Exercise 3: Solution
START
Input
gender, weight
False
Print
“Not Normal Weight”
END 62
Exercise 3: Solution
BEGIN
READ gender, weight
IF (gender = ‘F’) AND (weight >= 2.5) AND (weight <= 4.5) THEN
PRINT “Normal Weight”
ELSE IF (gender = ‘M’) AND (weight >= 4) AND (weight <= 5.5) THEN
PRINT “Normal Weight”
ELSE
PRINT “Not Normal Weight”
ENDIF
END
63
Exercise 3: Another Solution
START
Input
gender, weight
Print Print
“Not Normal Weight” “Normal Weight”
END
64
Exercise 3: Another Solution
BEGIN
READ gender, weight
IF ((gender = ‘F’) AND (weight >= 2.5) AND (weight <= 4.5)) OR
((gender = ‘M’) AND (weight >= 4) AND (weight <= 5.5)) THEN
PRINT “Normal Weight”
ELSE
PRINT “Not Normal Weight”
ENDIF
END
65
Exercise 4
• Algorithm for a calculator that works on integer
numbers. The user enters two numbers to perform
only one of four basic arithmetic operations (+, -, *
and /).
• The interaction with the user might look like this:
Enter your expression:
1 2 +
The result is: 3
• Hint: The user is allowed to do only one operation at
a time.
66
Exercise 4: Solution
• Using case structure
START
Input
num1, num2, op
Case of
op
‘+’ ‘-’ ‘*’ ‘/’ Otherwise
result = result = result = result = Print
num1+num2 num1-num2 num1*num2 num1/num2 “Invalid Operation”
Print
result
67
END
Exercise 4: Solution
BEGIN
PRINT “ Enter your expression: “
READ num1, num2 , op
CASE OF op:
= ‘+’:
result = num1 + num2
=‘-’:
result = num1 - num2
=‘*’:
result = num1 * num2
=‘/’:
result = num1 / num2
OTHERWISE:
PRINT “Invalid Operation”
END-OF-CASE
PRINT “The result is: ”, result
END
68
Exercise 5
• Algorithm for program that converts seconds to
equivalent hours, minutes and seconds.
69
Exercise 5: How to Solve?
totalSeconds = 4000
How to convert to hours, minutes and seconds?
70
Exercise 5: Solution
START
Input A BEGIN
totalSeconds READ totalSeconds
Print
Seconds = minutes
seconds = totalSeconds % 60
totalSeconds %60
PRINT “Seconds: ”, seconds
totalSeconds =
Print
totalSeconds / 60 totalSeconds = totalSeconds / 60
seconds minutes = totalSeconds % 60
hours =
PRINT “ , Minutes: ”, minutes
totalSeconds = totalSeconds
72
Exercise 6: Solution
START
Input BEGIN
first, second READ first, second
third = first
third = first
first = second
first = second
second = third
second = third
PRINT “The first number: ”, first
Print PRINT “The second number: ”, second
first, second
END
END
73
Thank You
74