Você está na página 1de 27

Introduction to

Flowcharting
A Supplement to
Starting Out with C++, 4th Edition
Figure 1-11: Process of system development

Building programs
•Edit
•Compile
•Link
•Run
Figure 1-11: Process of system development
Understand the problem:
•Input
•Output
•Process
Develop the solution
(Algorithm):
•Structure chart
•Pseudocode
•Flowchart
Converting design to
computer codes.

e.g:
Flowchart -> C program
Algorithm is the steps
to solve problems
Figure 1-12: Structure chart example
Pseudocode:

Describe an algorithm in English-like statements

Example: Algorithm for multiplying two


numbers

1. Get the first number, let say A


2. Get the second number, let say B
3. Calculate the result let say C, C= A * B
4. Display the result, C
Today’s Topics
•Flowchart Symbols
•Control Structures
Flowchart:

Represents an algorithm in graphical symbols

Example: Algorithm for multiplying two


numbers
Start

Get A
Get B

Calculate Resut
C=A*B

Display the
Result C

Stop
Flowchart Symbols
Terminal: Used to indicates the start and end of a
flowchart. Single flowline. Only one “Start” and “Stop”
terminal for each program. The end terminal for
function/subroutine must use “Return” instead of “Stop”.
Process: Used whenever data is being manipulated. One
flowline enters and one flowline exits.
Input/Output: Used whenever data is entered (input) or
displayed (output). One flowline enters and one flowline
exits.
Decision: Used to represent operations in which there are
two possible selections. One flowline enters and two
flowlines (labelled as “Yes” and “No”) exit.
Function / Subroutine: Used to identify an operation in a
separate flowchart segment (module). One flowline enters
and one flowline exits.
On-page Connector: Used to connect remote flowchart
portion on the same page. One flowline enters and one
flowline exits.
Off-page Connector: Used to connect remote flowchart
portion on different pages. One flowline enters and one
flowline exits.
Comment: Used to add descriptions or clarification.

Flowline: Used to indicate the direction of flow of control.


Example:
Start Terminal.
Start Program start
here

Read A
Input.
Read B Enter values for
A and B

Calculate Resut
C=A*B Process

Display the
Result C Output

Stop Terminal
Stop Program end
here
Example: Use of comments/description

Start

Read N, N = The number of students


M M = The number of subjects

Yes

No

Stop
Example: Use of connectors on the same page.

Start

1 2

1- connection on the same flowchart portion

2- connection on the different flowchart portion

Stop

2
Example: Use of connectors on the different page.
Page 1 Page 2

Start

2
1

Stop
Yes 1

No

2
The details (how the function works)
Example: Function-call example. we put in another flowchart.
This also known as
Note: Module = function = subroutine Function-Definition
Page 1 Start terminal for a Page 2
Function is different.
Start Do not use “Start”
AVRG ( result,n1, n2,n3)

Read
n1, n2 , n3

sum = n1+ n2+n3

Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart

result = sum/3
At this part,
we only know what
we want to do. But we Print
don’t know how to do it result

This part also known as Return


Function-Call

Stop
End terminal
must be “Return”
Control Structures

•Describe the flow of execution

•Basic types of control structure:


• Sequential
• Selection
• Repetition
Sequential Structure

Multiple statements considered as one statement

Statement simply means


command or instruction
statement

 statement

statement
Selection Structure

If
“do or don’t” (one-choice)

TRUE
condition
condition

FALSE

statement
 statement

If set condition is true, execute the statement, else do


nothing
Selection Structure (cont..)

If-else
(two-choices)
“do this or do that”

TRUE FALSE
condition
condition

Statement 1 Statement 2  statement

°
If set condition is true, execute the first statement, else
execute second statement
Selection Structure (cont..)

Nested if
(if within if)

FALSE
test1 FALSE
test1
TRUE
TRUE
FALSE

TRUE
test2
°  °
statement

Considered as
one statement ° °
it is an “one-choice” if
Selection Structure (cont..)

Complex if-else & if Statements

FALSE
condition
TRUE

statement

statement TRUE
condition

FALSE
statement

°
° Considered as one statement
Repeatition Structure

while Loop
(pre-test loop)

°
FALSE
condition
conditio
n


TRUE
statement
body of loop

While a set condition is true, repeat statement (body of loop)


Repeatition Structure (cont…)

do-while Loop
(post-test loop)

°
statement
 statement

condition
TRUE
FALSE

Do the statement (body of loop) while a condition is true


Repeatition Control Structure (cont…)

for Loop
(pre-test loop)

initialization

° FALSE
condition

TRUE
body of loop

increment

y
Example:

Start

Read
Length,
Input:
Width Length <- 5
Width <- 3

Calculate Area Process:


Area=Length * Width Area = 5 * 3 = 15

Calculate Perimeter Process:


Perimeter= Perimeter =
2 * (Width+Length) 2* (5+3) = 16
Output

Area: 15
Print
Area, Perimeter: 16
Perimeter

Stop
Example:
What is the Output of the following flowchart when the input Num= 10

Start
Enter a Number >> 10

Category A
Input:
Read Num
Num <- 10
Num = 10
10 > 0 ? => YES

Num>0? No

Print
"Category B"
Yes

Print
Output:
"Category A"
“Category A”

Stop
Example:
What is the Output of the following flowchart when the input is Num= 0

Start
Enter a Number >> 0

Category B
Read Num Input: Category A
Num <- 0
Num = 0 Output:
0 > 0 ? => NO “Category B”

Num>0? No

Print
"Category B"
Yes

Print
Output:
"Category A"
“Category A”

Stop
Example:
What is the Output of the following flowchart when the input is Num= 4

Start Variables
Variables
Variables(in
(in
(inmemory):
memory):
memory):

Num
Num
Num [[[ 444 ]]]
Read Num Input: Result
Result
Result [[[ 4
0710]
9 ]]] 0497 +++ 4312
Num <- 4 Count
Count [[[ 3
Count 420
1 ]]] 4312 --- 111

Initialize

Result=0
Count=Num

Enter a Number => 4


Count
Count
Count=
Count ===4
132
0
Print Count Count: 4
4
132>
>>>0
000?? ??=>
=>
=>YES
YES
YES
0 => YES
NO Count: 3
Count: 2
Result=Result + Count
Count>0? Yes Count: 1
Count=Count - 1
Count: 0
No
Result: 10

Print
Result

Stop
Example:
What is the Output of the following flowchart when the input is N = 6 10
Page 1 Page 2 5
average

Start
N=6
AVRG ( result,n1, n2,n3)

Read
N Sum = 10 + 5 + 6

sum = n1+ n2+n3

AVRG (average, 10, 5, N) average =


21/3

result = sum/3

Print
average
Output:
Average: 7
Return

Stop

Você também pode gostar