Académique Documents
Professionnel Documents
Culture Documents
to Programming: C++
Lecture 3
Introduc)on to Flowchar)ng
Dr. Dina Salah
Some of the slides in this lecture are based upon Appendix C from Star%ng Out with
C++: From Control Structures to Objects. Published by Addison-Wesley
Flowchart Examples
Example 1: Draw a owchart that reads two
numbers and print their sum.
Start
Read Num1
Read Num2
S=Num1+Num2
Print S
Stop
Example 2
Draw a owchart that reads characters )ll Z is
input.
Start
Read C
C=Z
Yes
Print End
Stop
No
Example 3
Draw a owchart to nd the average of 3
numbers.
Start
Read a, b, c
avg=(a+b+c)/3
Print avg
Stop
Example 4
Draw a owchart to compute the area of a
rectangle
Start
Read l, b
area=l*b
Print area
Stop
Example 5
Write a owchart to print the greatest of two
numbers
Start
Read a, b
Yes
a>b
Print a
No
Print b
End
Example 6
Draw a owchart to convert inches to
cen)meters
Start
I=C=0
Read I
C=2.54*I
Print C
Stop
Example 7
Draw a owchart to get the number of hours
worked and the pay rate and display their product
as the gross pay.
START
Display message How many
hours did you work?
Read Hours
Read PayRate
Multiply Hours by
PayRate. Store result in
GrossPay.
Display GrossPay
END
Stepping Through
the Flowchart
START
Display message
How many
hours did you
work?
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
Variable Contents:
Hours: ?
PayRate: ?
GrossPay: ?
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
16
Stepping Through
the Flowchart
START
Display message
How many
hours did you
work?
Step 1: An
Output
Operation
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
How many
hours did
you work?
Screen Output
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Variable Contents:
Hours: ?
PayRate: ?
GrossPay: ?
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
17
Stepping Through
the Flowchart
START
Display message
How many
hours did you
work?
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
How many
hours did
you work?
40
Step 2: An Input
Operation
(User types 40)
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Variable Contents:
Hours: 40
PayRate: ?
GrossPay: ?
Multiply Hours
by PayRate.
Store result in
GrossPay.
END
18
Stepping Through
the Flowchart
START
Display message
How many
hours did you
work?
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
How much
do you get
paid per
hour?
Screen Output
Step 3: An
Output
Operation
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Variable Contents:
Hours: 40
PayRate: ?
GrossPay: ?
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
19
Stepping Through
the Flowchart
START
Display message
How many
hours did you
work?
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
Read Hours
How much
do you get
paid per
hour? 20
Display message
How much do
you get paid per
hour?
Step 4: Input
Operation
(User types 20)
Variable Contents:
Hours: 40
PayRate: 20
GrossPay: ?
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
20
START
Display message
How many
hours did you
work?
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
Read Hours
How much
do you get
paid per
hour? 20
Display message
How much do
you get paid per
hour?
Read PayRate
Variable Contents:
Hours: 40
PayRate: 20
GrossPay: 800
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
21
Stepping Through
the Flowchart
START
Display message
How many
hours did you
work?
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then
insert it again.
Your gross
pay is 800
Screen Output
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Variable Contents:
Hours: 40
PayRate: 20
GrossPay: 800
Multiply Hours
by PayRate.
Store result in
GrossPay.
Step 6: An Output
Operation
Display
GrossPay
END
22
Example 8
Draw a owchart to read three numbers; num1,
num2 and num3.
Compute the sum, average and product of the
three numbers
Display the sum and average onto the monitor
24
Example 9
Write a owchart that reads two numbers and
displays the numbers read in descending
order.
Assignment
27
Assignment
Name 5 socware that can be used in drawing
owcharts