Sessions 2-3 (Algorithms and Flowchart)
Sessions 2-3 (Algorithms and Flowchart)
Outline
Algorithm Basic Blocks Conditional Statements Examples of Algorithm Design Flowcharts Flowchart Symbols Flowchart Examples Arrays in Algorithms
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 2
Calculation Statements
Assignment statements
Explanation Statements
Begin, End
Input/Output Statements
Conditional Statements
Conditional statements:
Simple conditional statement: If <one or more condition> then <one or more statements> Changes the order of statements execution Start I 10 Write I in standard output I I+2 If (I <=98) then go to line 3 End
1. 2. 3. 4. 5. 6.
Value of I 1 3 5 7 9
Output 1 3 5 7
n 8
Trace Result
Run 1 2 3 502
Trace Result
N 2 4 6 8
i 1 5 8
j 2 7 0
k 3 1 -1
t 6 13 7
Output 6 13 7
Algorithm Design
Design an algorithm which gets a natural number, N, and determines if is it complete or not?
Complete number (N): If the sum of all the denominators (less than N) equals to N itself. for example: 6 = 1 + 2 + 3 1. Start Trace Result 2. Read N N I R S Output 3. S 0
4. 5. 6. 7. 8. 9. I1 R N-I*[N/I] If (R=0) then S S+I II+1 If ( I <= N/2) then go to line 5 If (S = N ) then write N is complete else write N is not complete 10. End
14 1 2 3 4 5 6 7 8 0 0 2 2 4 2 0 0 1 3 3 3 3 3 10
14 is not complete
10
Flowcharts
Flowchart: A set of graphical symbols to give a simple description of algorithms.
Design algorithm Draw flow chart Convert flow chart to programming language statements
12
Flowchart Symbols
Start and End
Start End
use arrows to connect components of a flow chart the end symbol can have multiple input arrows
Write R
13
N<5
zero
N
false
true
Continue
If the chart is to complex You need to cut it some where and continue it in the another partition
A A
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 14
Flowchart Examples
A very simple example:
Flowchart for an algorithm which gets two numbers and prints sum of their value Start 1. Start 2. Read A and B Read A , B 3. C A + B 4. Print C CA+B 5. End
Print C
End
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 15
1. 2. 3. 4. 5. 6.
Flowchart which calculates the real roots of a quadratic equation and then prints them: ax2+bx+c=0
Start Read a If ( a =0) then go to line 2 Read C and b D b2-4ac If ( D >0) then
Start Read a
a=0
no
Yes
7.
6.1. X1 (-b+D)/2a and X2 (-b-D)/2a 6.2. Write X1, X2 6.3. go to line 9 7.1. X -b/(2a) 7.2. Write X 7.3. go to line 9
Read c,b
If ( D = 0) then
8. 9.
write no root
A
16
I<=98 no End
Sharif University of Technology
17
A
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 18
B A
I<N/2 no write N is prime End
R=0 no
I I+1
A
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 19
11.
End
20
Arrays in Algorithms
Design an algorithm to convert a decimal number, n, to binary format:
division idea
12 12 0 2 6 6 0 2 3 2 1 2 1 0 1
1. 2. 3. 4. 5. 6. 7.
The result will be (0011) instead of (1100) We need a mechanism to save intermediate results
21
Array (indexed variable): A set of variables with the same name but different indices In your algorithms you can refer to A1 as A(1)
A A1 A3 A5 A2 A4
22
Start Read N I1 If N<2I the go to line 7 I I+1 Go to line 4 Use I variables with the name A: (A1,, AI) J1 R N-2*[N/2] A(J) R N [N/2] If (N>0) then Write A(J) J J-1 If J>0 the go to line 13 End
This line is only used for better understanding, you should not use them in your algorithms
23
A
A(J) R N [N/2]
J1
B
R N-2*[N/2] J>0 no
End
24