Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COMPUTER
PROGRAMMING
1
1
Flowcharts
ALGORITHM
An informal definition of an algorithm is:
ALGORITHM
A step-by-step problem-solving procedure
TOOLS
There are two commonly used tools to help to
document program logic (the algorithm)
Flowcharts
Pseudo code
5
FLOWCHART
Definitio
n
A flowchart
indicates:
The steps to be taken in order to solve a problem.
The order or the sequence of these steps.
FLOWCHART RULES
1. Use only one start and one stop per flowchart, --that is, one
way in and one way out of the flowchart.
2. The logic flow of the solution is displayed from top to
bottom and from left to right.
3. Use the appropriate symbol for each type of operation.
4. Use arrows when moving to another part of the
flowchart rather than lines.
5. Do not leave dead-ends--that is, a part of a question
unanswered.
SYMBOLS
Symbol
Description
TERMINAL - To start or end a flowchart
INPUT / OUTPUT - Used with Read, Input, Print and other I/O
commands.
PROCESSING - Used for operations done inside the computer. Such
as calculations, storing and moving of data.
DECISION - Used to ask a question in programming. Questions are
Yes/No format (Used with the If Statement).
Step 1- Start
Step 2- Print "Hello,
World"
Step 3- Stop
10
Print
Hello, World
Stop
11
12
Algorithm
A: First Number
Step 1- Start
B: Second
Number
Step 2- Read
A
C: Sum (A+B)
Step 3- Read B
Step 4- Calculate C =
A+B
Step 5- Print C
Step 6- Stop
13
Start
Read A
Read B
C= A+B
Print C
14
Stop
15
Algorithm
Y: Second Number
Step 1Start
Step 2- Read X, Y, Z
Z: Third Number
S: Sum (X+Y+Z)
X: First Number
A: Average (S/3)
P: Product
(X*Y*Z)
16
Start
Read X,Y,Z
S= X+Y+Z
A=S/3
P=X*Y*Z
Print
S,A,P
Stop
17
18
Algorithm
Step 1Start
Step 2- Read N1, N2
Step 3- Calculate D = N1-N2
Step 4- Calculate V =
N1/N2
Step 5- Print D,V
Step 6- Stop
19
20
Exercise
Algorithm
Step 1Start
Step 2- Read
R
Step 3- Calculate A = PI*(R)2
Step 4- Calculate C =
2*PI*R
Step 5- Print R, A, C
Step 6- Stop
22
2. BRANCHED FLOWCHARTS
Example
1
F(x) =
X X>=0
{
-X
X<0
23
2. BRANCHED FLOWCHARTS
Variables
X : Number
F: function of X
Algorithm
Step 1Start
Step 2- Read X
Step 3- if X >=0 then F =X
Step 4- if X <0 then F =-X
Step 5- Print F
Step 6- Stop
24
2. BRANCHED FLOWCHARTS
Start
Flowchart
Read X
NO
X>=0
YES
F=X
F=-X
Print F
Stop
25
2. BRANCHED FLOWCHARTS
Example
2
2. BRANCHED FLOWCHARTS
Start
Flowchart
Read X
0>
W=2*X-1
>0
?X
=0
W=X+1
W=SIN(X)+5
Print X,W
27
Stop
2. BRANCHED FLOWCHARTS
Result
When X=20
When X=-10
X= 20
W= 21
X= -10
W= -21
28
2. BRANCHED FLOWCHARTS
Example
3
Exercise
29
2. BRANCHED FLOWCHARTS
Variables
C : Traffic light color
Algorithm
Step 1Start
Step 2- Read
C
Step 3- make a Decision (what is c)
Step 4- if C is RED then Print
STOP
Step 5- if C is YELLOW then Print
WAIT
Step 6- if C is GREEN then Print
PASS
Step 7- Stop
30
3. LOOP FLOWCHARTS
Example
1
31
3. LOOP FLOWCHARTS
Flowchart
Start
N=1
Print N
F
While
N<=7
T
N=N+3
Stop
32
3. LOOP FLOWCHARTS
Result
Loop
3
33
3. LOOP FLOWCHARTS
Example
2
34
3. LOOP FLOWCHARTS
Flowchart
Start
i=0
Sum=0
While
i<10
T
Read X
Sum= X + Sum
Increment i
F
avg=Sum/10
Print avg
Stop
35
3. LOOP FLOWCHARTS
Result
Loop
Read X
Sum
1
2
3
3
4
1
3
7
8
1
2
3
10
18
25
30
33
41
9
10
4
5
45
50
9
10
Avg =50/10 =5
36