[go: up one dir, main page]

0% found this document useful (0 votes)
91 views37 pages

Introduction To Flowcharting: CS110: Programming and Problem Solving For Engineering

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

Introduction To Flowcharting: CS110: Programming and Problem Solving For Engineering

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

Introduction to

Flowcharting

CS110: Programming and Problem


Solving for Engineering

1
Acknowledgment

This tutorial is based upon Appendix C from


Starting Out with C++:
From Control Structures to Objects
(5th Edition)

Copyright Tony Gaddis 2007

Published by Addison-Wesley
2
START

What is a Flowchart? Display message


“How many
hours did you
work?”

• A flowchart is a Read Hours

diagram that depicts Display message


“How much do
the “flow of control” you get paid per
hour?”

of a program.
Read PayRate

Multiply Hours
by PayRate.
Store result in
GrossPay.

Display
GrossPay

END
3
Basic Flowchart START
Rounded
Rectangle

Symbols Display message


“How many
hours did you
work?”

• Notice there are three Read Hours

types of symbols in this Display message


“How much do
Parallelogram
flowchart: you get paid per
hour?”

– rounded rectangles
Read PayRate
– parallelograms
– a rectangle Multiply Hours
by PayRate.
Rectangle
• Each symbol represents
Store result in
GrossPay.

a different type of Display


Rounded GrossPay
operation. Rectangle
END
4
Basic Flowchart START Terminal

Symbols Display message


“How many
hours did you
work?”

• Terminals Read Hours

– represented by rounded Display message


“How much do
rectangles 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
5
Basic Flowchart START

Symbols Display message


“How many
hours did you
work?”

• Input/Output Operations Read Hours

– represented by Display message


“How much do Input/Output
parallelograms you get paid per
Operation
hour?”
– 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

work?” GrossPay

END
6
Basic Flowchart START

Symbols Display message


“How many
hours did you
work?”

• Processes Read Hours

– represented by rectangles Display message


“How much do
– indicates a process such as you get paid per
hour?”
a mathematical
computation or variable Read PayRate

assignment
Multiply Hours
by PayRate.
Process Store result in
Multiply Hours GrossPay.
by PayRate.
Store result in Display
GrossPay
GrossPay.
END
7
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

In the next seven slides we Read Hours


will step through each
symbol in the flowchart. We Display message
“How much do
will show the program you get paid per
hour?”
output and the contents of
the variables. Read PayRate

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

Hours: ? Display
PayRate: ? GrossPay

GrossPay: ? END
8
Stepping Through START Step 1: An
Output
Display message Operation
the Flowchart “How many
hours did you
work?”

Screen 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: ? Display
PayRate: ? GrossPay

GrossPay: ? END
9
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

How many
Step 2: An 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
Variable Contents: GrossPay.
The value 40 is stored in Hours.
Hours: 40
Display
PayRate: ? GrossPay

GrossPay: ? END
10
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

Screen Output Read Hours


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

Read PayRate

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

Hours: 40
Display
PayRate: ? GrossPay

GrossPay: ? END
11
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

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

Step 4: Input Read PayRate


Operation
(User types 20) Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: 40
The value 20 is stored in PayRate. Display
PayRate: 20 GrossPay

GrossPay: ? END
12
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

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

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

Hours: 40
Display
PayRate: 20 The value 800 is stored
GrossPay

GrossPay: 800 in GrossPay.


END
13
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

Screen Output 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
Step 6: An Output Display
PayRate: 20 Operation
GrossPay

GrossPay: 800 END


14
Three Flowchart Structures
• Sequence
• Selection
• Iteration

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

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

17
Selection 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

18
Selection 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

19
Selection 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.

20
Selection Structure
• The flowchart segment below shows a decision structure
with only one action to perform. It is expressed as an if
statement in C++ code.
Flowchart C++ Code

NO YES if (x < y)
x < y? a = x * 2;

Calculate a
as x times 2.

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

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

YES
x < y? Process A

24
Iteration Structure
• The flowchart segment below shows an iteration structure
expressed in C++ as a while loop.

Flowchart C++ Code

while (x < y)

YES x++;
x < y? Add 1 to x

25
Controlling an Iteration Structure
• The action performed by an iteration 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
YES
flowchart be modified so x < y? Display x
it is no longer an infinite
loop?

26
Controlling an Iteration Structure
• ANSWER: By adding an action within the iteration that
changes the value of x.

YES
x < y? Display x Add 1 to x

27
A Pre-Test Iteration Structure
• 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

28
A Pre-Test Iteration Structure
• In a pre-test iteration 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 Iteration Structure
• This flowchart segment shows a post-test
iteration structure.
• The condition is tested AFTER the actions Display x
are performed.
• A post-test iteration structure always Add 1 to x
performs its actions at least once.

YES
x < y?

30
A Post-Test Iteration Structure
• The flowchart segment below shows a post-test iteration
structure expressed in C++ as a do-while loop.

C++ Code
Display x
do
{
Flowchart cout << x << endl;
Add 1 to x x++;
} while (x < y);

YES
x < y?

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

32
Connectors

•The “A” connector


A
indicates that the second START

flowchart segment begins


where the first segment
ends.

END
A

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

34
Modules
START
•The position of the module
symbol indicates the point the Read Input.
module is executed.
•A separate flowchart can be Call calc_pay
function.
constructed for the module.

Display results.

END

35
Combining Structures
• Structures are commonly combined to create more
complex algorithms.
• The flowchart segment below combines a selection
structure with a sequence structure.

YES
x < y? Display x Add 1 to x

36
Combining Structures
• This flowchart segment
shows two selection NO YES
structures combined. x > min?

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

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

37

You might also like