Vous êtes sur la page 1sur 28

Introduc)on

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

Display message How much


do you get paid per hour?

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.

In the next seven slides we


will step through each
symbol in the flowchart. We
will show the program
output and the contents of
the variables.

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.

The value 40 is stored in Hours.


Display
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: ?

The value 20 is stored in PayRate.

Read PayRate

Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay

END

20

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

Step 5: The product of


Hours times PayRate is
stored in GrossPay

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.

The value 800 is stored


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

Draw a owchart to do the following:


1. Take a temperature in Fahrenheit (F) convert it to Cen)grade
(C). The formulas is as follows:
C = ( F - 32) / 1.8
Output the results with a message that states:
___ degrees C is equal to ___ degrees F

2. Read records from a le containing the name and scoring
average of all players in the Egyp)an na)onal football league.
Write out the name and average of every player with an average
of 5 goals or greater.
3.Read a le containing the high temperature of each day in
December. Write out the average high temperature for the
month. You can assume the input le includes only the 31
temperatures for December.

27

Assignment
Name 5 socware that can be used in drawing
owcharts

Vous aimerez peut-être aussi