Professional Documents
Culture Documents
04 Flowcharts 3
04 Flowcharts 3
Ibrahim Mesecan
Florenc Skuka
Page 1
LESSON 3:
FLOWCHARTS - 2
Ibrahim Mesecan
Florenc Skuka
Page 2
FLOWCHARTS
Mainly, flowcharts flow on a line.
Start
Get Data
Calculate
Results
Show Results
Ibrahim Mesecan
Florenc Skuka
Page 4
End
Flowcharts
FLOWCHARTS
But sometimes, when processing some steps,
there can be different branching and
jumping from one path to another inside the
module.
As a result, every module
should have one entry (A)
A
and one output line (B)
Calculate
Results
B
Ibrahim Mesecan
Florenc Skuka
Page 5
Flowcharts
Logical
Expr.
Flowcharts
True
B False
Ibrahim Mesecan
Florenc Skuka
Page 7
Ibrahim Mesecan
Florenc Skuka
Page 8
True
!A
A && B
A || B
False
False
True
False
False
False
True
True
False
True
True
False
False
False
True
True
True
False
True
True
Flowcharts
Flowcharts
FLOWCHARTS - DECISION
Design an algorithm to determine the state of water
water_temp<=0 Solid
0 < water_temp<=100 Liquid
100<water_temp Gaseous
Start
Get Data
Determine
the state
Show State
End
Ibrahim Mesecan
Florenc Skuka
Page 11
Flowcharts
FLOWCHARTS - DECISION
Then the standard algorithm can be
Ibrahim Mesecan
Florenc Skuka
Page 12
Flowcharts
FLOWCHARTS - DECISION
We can also take advantage of the structures we
use. For example the following operations, circled
with red, can be discarded if the decision structure
is changed
Ibrahim Mesecan
Florenc Skuka
Page 13
Flowcharts
FLOWCHARTS - DECISION
To simplify the case, lets have a
look how it would work if we just
had two conditions.
temp<=0 Ice
0<temp<100 Liquid
Ibrahim Mesecan
Florenc Skuka
Page 14
Flowcharts
FLOWCHARTS - DECISION
To simplify the case, lets have a
look how it would work if we just
had two conditions.
temp<=0 Solid
0<temp<100 Liquid
Ibrahim Mesecan
Florenc Skuka
Page 15
FLOWCHARTS - DECISION
Now, if the third logical check is
added to the false path of the
second decision, automatically
can be concluded that (temp is
not less than 0 and temp is not
less than 100) temp>=100.
As it can be seen from the
schema, the True paths are
connected to after the last
conclusion (point B). That
means the second expression is
nested in the first decision
structure.
Ibrahim Mesecan
Florenc Skuka
Page 16
Flowcharts
B
Nested
Structure
FLOWCHARTS- DECISION
Determine the letter grade of a student.
0..59
60..69
70..79
80..89
90..100
F
D
C
B
A
Ibrahim Mesecan
Florenc Skuka
Page 17
Flowcharts
LOOPS
Sometimes, we need to repeat some operations several
times. In such cases, we can use loops.
Loops have
One entry point (A)
One quit point (B)
Body of the loop and
A decision statement.
Pretest Loop
Body of
the loop
B
B
Ibrahim Mesecan
Florenc Skuka
Page 18
Continue with
other processes
Posttest Loop
Flowcharts
LOOPS
There are two essential points in the
loop,
the decision statement that decides
whether to continue or not.
The loop control variable
The loop control variable (LCV) has to
be changed somewhere in the loop
And, while the condition is satisfied
the process is repeated.
If never changes, the loop continues
infinitely. This error is called Infinite
Loop error.
Ibrahim Mesecan
Florenc Skuka
Page 19
Flowcharts
DETERMINING LOOPS
When developing algorithms, several
steps are written down.
At this level, the variables are named
The repeating body is identified
The process
The loop control variable (LCV) and how is
modified in the body
The loop quit condition
are identified.
B
Ibrahim Mesecan
Florenc Skuka
Page 20
Flowcharts
DETERMINING LOOPS
Ex. : Calculate the sum of the numbers from m to n;
Name the variables:
Flowcharts
DETERMINING LOOPS
E.x. : Calculate the sum of the numbers from 21 to 24
Cnt= 21;
Sum = Sum + Cnt 21; Cnt = Cnt + 1 22
Check if Cnt >24
Sum = Sum + Cnt 43; Cnt = Cnt + 1 23
Check if Cnt >24
Sum = Sum + Cnt 66; Cnt = Cnt + 1 24
Check if Cnt >24
Sum = Sum + Cnt 90; Cnt = Cnt + 1 25
Check if Cnt >24 Yes Quit
Show Result (Sum is 90)
Ibrahim Mesecan
Florenc Skuka
Page 22
Flowcharts
DETERMINING LOOPS
The Algorithm
Ibrahim Mesecan
Florenc Skuka
Page 23
Flowcharts
DETERMINING LOOPS
Ex : Find out the max number given by user. Read the
numbers as far as the given numbers are greater than 0.
Name the variables: One variable to keep max: Max;
Another var. to get the number entered by the user: Num
Initial values: Num: Doesnt matter, Max: must be
initialized to 0; Max=0
Number Entered (Num)
Max
Continue while Num >0
3
3
( Quit when Num<=0 )
5
5
Ibrahim Mesecan
Florenc Skuka
Page 24
Flowcharts
Quit
DETERMINING LOOPS
Then the algorithm can be summarized:
Ibrahim Mesecan
Florenc Skuka
Page 25
Flowcharts
DETERMINING LOOPS
Then the algorithm can be summarized:
Initialize Max;
Start point for the loop
The Loop
Flowcharts
DETERMINING LOOPS
Ex.2: Calculate and show the Greatest Common Divisor
(GCD) of two numbers given.
Euclidean Algorithm:
There are two numbers: Big and Small,
While Small is different than zero:
calculate the remainder: Rem=Big % Small
Then assign Small to Big then assign Rem to
Small (Big=Small; Small=Rem)
When Small is zero, the Big is the GCD.
Ibrahim Mesecan
Florenc Skuka
Page 27
Flowcharts
DETERMINING LOOPS
Euclidean Algorithm
E.g. 1: Calculate the GCD of 24 and 18;
Rem = 24 % 18
6 Big = 18; Small= 6
Rem = 18 % 6
0 Big =6; Small =0
GCD
E.g. 2: Calculate the GCD of 26 and 19;
Rem = 26 % 19
7 Big = 19; Small= 7
Rem = 19 % 7
5 Big =7; Small =5
Rem = 7 % 5
2 Big =5; Small =2
Rem = 5 % 2
1 Big =2; Small =1
Rem = 2 % 1
0 Big =1; Small =0
Ibrahim Mesecan
Florenc Skuka
Page 28
GCD
Flowcharts
DETERMINING LOOPS
Euclidean Algorithm
Name the variables: Big, Small, and Rem
Initial values: Get the numbers: Big and Small
Continue while Small != 0
( Quit when Small==0 )
The Process:
Rem=Big % Small;
Big = Small; Small = Rem
Ibrahim Mesecan
Florenc Skuka
Page 29
Flowcharts
DETERMINING LOOPS
Euclidean Algorithm
Ibrahim Mesecan
Florenc Skuka
Page 30
Flowcharts