0% found this document useful (0 votes)
19 views

01 Flowchart

Slide talk about Flowchart in programing.

Uploaded by

hohuuloc40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

01 Flowchart

Slide talk about Flowchart in programing.

Uploaded by

hohuuloc40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Introduction to Flowcharting

This slide is based upon Appendix C from Starting Out with C++: From
Control Structures to Objects (5th Edition), Tony Gaddis 2007,
Published by Addison-Wesley
Contents
o Flowchart
o Basic flowchart symbols
o Stepping through the flowchart
o Three flowchart structures
o Examples
o Exercises

fit@hcmus | Programming 1 | 2022 2


START

What is a Flowchart? Display


message “How
many hours did
you work?”

o A flowchart is a diagram that Read Hours

depicts the “flow of control” of a


program. Display message
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate.
Store result in
GrossPay.

Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 3


START

What is a Flowchart? Display message


“How many
hours did you
work?”

o A flowchart Read Hours

• shows logic of an algorithm


Display message
• emphasizes individual steps “How much do
you get paid per
hour?”
and their interconnections
e.g. control flow from one action Read PayRate

to the next
Multiply Hours
by PayRate.
Store result in
GrossPay.

Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 4


Rounded START
Rectangle

Basic Flowchart Symbols Display


message “How
many hours did
you work?”

o Three types of symbols in this Read Hours

flowchart:
Display message
• rounded rectangle “How much do
you get paid per Parallelogram
hour?”
• parallelogram
• rectangle Read PayRate

Multiply Hours
by PayRate.
Rectangle Store result in
o Each symbol represents a GrossPay.

different type of operation. Display


Rounded GrossPay
Rectangle
END

fit@hcmus | Programming 1 | 2022 5


Terminal START

Basic Flowchart Symbols Display


message “How
many hours did
you work?”

o Terminals Read Hours

• represented by rounded rectangles


Display
• indicate a starting or ending point message “How
much do you get
paid per hour?”

Read PayRate
START
Multiply Hours
by PayRate.
Store result in
GrossPay.

END
Display
GrossPay
Terminal
END

fit@hcmus | Programming 1 | 2022 6


START

Basic Flowchart Symbols Display


message “How
many hours did
you work?”

o Input/Output Operations Read Hours

• represented by parallelograms
Display
• indicate an input or output operation message “How
much do you get
Input/Output
paid per hour?” Operation

Read PayRate

Display
Multiply Hours
message “How by PayRate.

many hours did Read Hours Store result in


GrossPay.

you work?”
Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 7


START

Basic Flowchart Symbols Display


message “How
many hours did
you work?”

o Processes Read Hours

• represented by rectangles
Display
• indicates a process such as a message “How
much do you get
paid per hour?”
mathematical computation or
variable assignment Read PayRate

Multiply Hours by Multiply Hours


PayRate. Store result in by PayRate.
Process Store result in
GrossPay. GrossPay.

Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 8


START

Stepping Through the Flowchart Display


message “How
many hours did
you work?”

We will step through each symbol in the


flowchart. We will show the program output Read Hours
and the contents of the variables.
Display
message “How
much do you get
paid per hour?”

Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.
Hours: ?
PayRate: ?
GrossPay: ? Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 9


START Step 1: An
Output

Stepping Through the Flowchart Display


message “How
many hours did
Operation

you work?”

Screen Read Hours


How many
hours did Output
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: ?
GrossPay: ? Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 10


START

Stepping Through the Flowchart Display


message “How
many hours did
you work?”

How many
Step 2: An Input Read Hours
hours did Operation
you work?
(User types 40) Display
40 message “How
much do you get
paid per hour?”

Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.
Hours: 40
PayRate: ? The value 40 is stored in
GrossPay: ? Hours. Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 11


START

Stepping Through the Flowchart Display


message “How
many hours did
you work?”

Screen Read Hours


How much
do you get Output
paid per
Display
hour? message “How
Step 3: An much do you get
Output paid per hour?”
Operation

Read PayRate

Multiply Hours
by PayRate.
Variable Contents: Store result in
GrossPay.
Hours: 40
PayRate: ?
GrossPay: ? Display
GrossPay

END

fit@hcmus | Programming 1 | 2022 12


START

Stepping Through the Flowchart Display


message “How
many hours did
you work?”

Read Hours
How much
do you get
paid per
Display
hour? 20 message “How
much do you get
paid per hour?”

Step 4: Input Read PayRate


Operation
(User types Multiply Hours
20) by PayRate.
Variable Contents: Store result in
GrossPay.
Hours: 40
PayRate: 20
GrossPay: ? The value 20 is stored in Display
PayRate. GrossPay

END

fit@hcmus | Programming 1 | 2022 13


START

Stepping Through the Flowchart Display


message “How
many hours did
you work?”

Read Hours
How much
do you get
paid per
Display
hour? 20 message “How
Step 5: The product much do you get
paid per hour?”
of Hours times
PayRate is stored in
GrossPay
Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.
Hours: 40
PayRate: 20
GrossPay: 800 Display
GrossPay
The value 800 is
stored in GrossPay.
END

fit@hcmus | Programming 1 | 2022 14


START

Stepping Through the Flowchart Display


message “How
many hours did
you work?”

Screen Read Hours


Your gross
pay is 800 Output
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
GrossPay: 800 Step 6: An Display
GrossPay
Output Operation

END

fit@hcmus | Programming 1 | 2022 15


Three Flowchart Structures
▪ Sequence
▪ Selection
▪ Iteration

fit@hcmus | Programming 1 | 2022 16


Sequence Structure
o A series of actions are performed in sequence
o The pay-calculating example was a sequence flowchart.

fit@hcmus | Programming 1 | 2022 17


Selection Structure
o One of two possible actions is taken, depending on a condition.

fit@hcmus | Programming 1 | 2022 18


Selection Structure
o 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

fit@hcmus | Programming 1 | 2022 19


Selection Structure
o 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

fit@hcmus | Programming 1 | 2022 20


Selection Structure
o The flowchart segment below shows a decision structure is
expressed in C++ as an if/else statement.

NO YES
x < y?

Calculate a Calculate a as
as x plus y. x times 2.

fit@hcmus | Programming 1 | 2022 21


Iteration Structure
o An iteration structure represents part of the program that repeats.
This type of structure is commonly known as a loop.

fit@hcmus | Programming 1 | 2022 23


Iteration Structure
o Notice the use of the diamond symbol.
o 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.

fit@hcmus | Programming 1 | 2022 24


Iteration Structure
o 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 iteration stops and the structure is
exited.

YES
x < y? Process A

fit@hcmus | Programming 1 | 2022 25


Controlling an Iteration Structure
o The action performed by an iteration structure MUST eventually
cause the loop to terminate.
o Otherwise, an infinite loop is created.

o In this flowchart segment, x is never changed. Once the loop starts,


it will never end.
YES
x < y? Display x

fit@hcmus | Programming 1 | 2022 27


Controlling an Iteration Structure
o x is never changed. Once the loop starts, it will never end.

o How can this flowchart be modified so it is no longer an infinite


loop?

YES
x < y? Display x

fit@hcmus | Programming 1 | 2022 28


Controlling an Iteration Structure
o By adding an action within the iteration that changes the value of x.

YES
x < y? Display x Add 1 to x

fit@hcmus | Programming 1 | 2022 29


A Pre-Test Iteration Structure
o This type of structure is known as a pre-test iteration structure. The
condition is tested BEFORE any actions are performed.

YES
x < y? Display x Add 1 to x

o if the condition does not exist, the loop will never begin.

fit@hcmus | Programming 1 | 2022 30


A Post-Test Iteration Structure
o This flowchart segment shows a post-test
iteration structure. Display x

o The condition is tested AFTER the actions


are performed. Add 1 to x

o A post-test iteration structure always


performs its actions at least once. YES
x < y?

fit@hcmus | Programming 1 | 2022 31


Connectors
o Sometimes a flowchart will not fit on one page.
o A connector (represented by a small circle) allows you to connect
two flowchart segments.

fit@hcmus | Programming 1 | 2022 33


Connectors
o The “A” connector indicates that the second flowchart segment
begins where the first segment ends.

START A

END
A

fit@hcmus | Programming 1 | 2022 34


Modules
o A program module, such as a subprogram (or function in C++), is
represented by a special symbol.

fit@hcmus | Programming 1 | 2022 35


Modules
START

• The position of the module symbol


indicates the point the module is Read Input.

executed.
Call calc_pay
function.
• A separate flowchart can be constructed
for the module. Display results.

END

fit@hcmus | Programming 1 | 2022 36


Combining Structures
o This flowchart
segment shows two NO YES
selection structures x > min?

combined.
Display “x is NO YES
outside the limits.”
x<
max?

Display “x is Display “x is
outside the limits.” within limits.”

fit@hcmus | Programming 1 | 2022 37


Examples

fit@hcmus | Programming 1 | 2022 38


Example 01
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

fit@hcmus | Programming 1 | 2022 39


Example 01
Step 1: Input M1,M2,M3,M4 START

Step 2: GRADE  (M1+M2+M3+M4)/4 Input


M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL” GRADE(M1+M2+M3+M4)/4

else
Y GRADE<5 N
Print “PASS” 0

endif
Print “FAIL” Print “PASS”

STOP

fit@hcmus | Programming 1 | 2022 40


Example 02
o Write an algorithm and draw a
flowchart to convert the length in feet
to centimeter.

o Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm
(Lcm) by multiplying Lft with
30
Print length in cm (Lcm)

fit@hcmus | Programming 1 | 2022 41


Example 02
o Write an algorithm and draw a START

flowchart to convert the length in feet Input

to centimeter. Lft

Lcm  Lft x 30

o Pseudocode:
Print
Input the length in feet (Lft) Lcm

Calculate the length in cm


(Lcm) by multiplying Lft with STOP
30
Print length in cm (Lcm)

fit@hcmus | Programming 1 | 2022 42


Example 03
o Write an algorithm and draw a flowchart that will read the two
sides of a rectangle and calculate its area.

fit@hcmus | Programming 1 | 2022 43


Example 03
o Write an algorithm and draw a flowchart that will calculate the
roots of a quadratic equation.

fit@hcmus | Programming 1 | 2022 44


Example 03
o Write an algorithm and draw a flowchart that will calculate the
roots of a quadratic equation.

o Hint:
d = sqrt(b2 - 4ac),
if d < 0, there is no root.
else if d == 0, x1 = x2 = -b/2a
else the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

fit@hcmus | Programming 1 | 2022 45


Example 04
o Write an algorithm that reads two values, determines the largest
value and prints the largest value with an identifying message.

fit@hcmus | Programming 1 | 2022 46


Example 05
o Write an algorithm that reads three numbers and prints the value
of the largest number.

fit@hcmus | Programming 1 | 2022 47


Exercises

fit@hcmus | Programming 1 | 2022 48


Exercises
o Check whether an input year is a leap year.
• Ref: https://en.wikipedia.org/wiki/Leap_year#Algorithm

fit@hcmus | Programming 1 | 2022 49


Exercises
o Electricity cost calculator:
• Ref: https://www.evn.com.vn/c3/evn-va-khach-hang/Bieu-gia-ban-le-dien-
9-79.aspx
• Ref: https://www.evn.com.vn/c3/calc/Cong-cu-tinh-hoa-don-tien-dien-9-
172.aspx

fit@hcmus | Programming 1 | 2022 50


Questions and Answers

fit@hcmus | Programming 1 | 2022 51

You might also like