Date post: | 14-Apr-2018 |
Category: |
Documents |
Upload: | shubham-sharma |
View: | 226 times |
Download: | 0 times |
of 24
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
1/24
1
Introduction to Programming
Sessions 2 & 3: Introduction to Algorithms and Flowchart
Instructor: Arash [email protected]
Sharif University of TechnologyDepartment of Computer Engineering
Spring 2008
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
2/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
2
Outline
Algorithm Basic Blocks
Conditional Statements
Examples of Algorithm Design
Flowcharts Flowchart Symbols
Flowchart Examples
Arrays in Algorithms
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
3/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
3
Basics of Algorithms
4 types of statements :
Conditional Statements
If statements
Calculation Statements
Assignment statements
Explanation Statements
Begin, End
Input/Output Statements
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
4/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
4
Conditional Statements
Conditional statements:
Simple conditional statement: If then Changes the order of statements execution
An algorithm to calculate even numbersbetween 10 and 99
1. Start2. I 103. Write I in standard output
4. I I+25. If (I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
5/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
5
Conditional Statements (contd)
Design an algorithm which gets a natural value, n,
as its input and calculates odd numbers equal orless than n. Then write them in the standardoutput:
1. Start
2. Read n3. I 1
4. Write I
5. I I + 2
6. If ( I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
6/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
6
Conditional Statements (contd)
Second type of conditional statements:
If Then Else Design an algorithm which generates even numbers
between 1000 and 2000 and then prints them in thestandard output. It should also print total sum:
1. Start2. I 1000 and S 03. Write I4. S S + I5. I I + 2
6. If (I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
7/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
7
Conditional Statements (contd)
Design an algorithm with a natural number, n, as its
input which calculates the following formula andwrites the result in the standard output:
S = + + +1/n
1. Start
2. Read n3. I 2 and S 0
4. S= S + 1/I
5. I I + 2
6. If (I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
8/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
8
Conditional Statements (contd)
Loop: A sequence of statements which will
be repeated in the algorithm execution.
Counter: A variable which defines thenumber of repeats.
Increment or decrement counter in eachexecution
Check the value of the counter to find out end ofexecution
Usually named: I
Most of previous examples
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
9/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
9
Conditional Statements (contd)
Determine the value of loop counter in each step of the
following algorithm:1. Start
2. N 2
3. Read i, j and k
4. t i + j + k5. Write t
6. N N+2
7. If (N
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
10/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
10
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
2. Read N
3. S 04. I 1
5. R N-I*[N/I]
6. If (R=0) then S S+I
7. I I + 1
8. If ( I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
11/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
11
Algorithm Design (contd)
Design an algorithm which gets a natural number, n,and determines that if it is prime or not? what is the idea?1. Start2. Read N3. I 2
4. R N-I*[N/I]5. If ( R = 0) then write N is not prime
and go to line 96. I I + 1
7. If ( I < N) then go to line 48. Write N is prime9. End This algorithm works for numbers greater than 2
N/2 or N could beused but N
reduces numberof iteration
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
12/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
12
Flowcharts
Flowchart: A set of graphical symbols to
give a simple description of algorithms. Design algorithm
Draw flow chart
Convert flow chart to programming languagestatements
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
13/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
13
Flowchart Symbols
Start and End
use arrows to connect components of a flow chart
the end symbol can have multiple input arrows
Assignment and Process
Input and Output
Start
End
R [N/I] A 2 S S+I
Read N Write R
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
14/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
14
Flowchart Symbols (contd)
Conditional Decision Symbol
multiple input or output arrows
Continue If the chart is to complex
You need to cut it some where and continue it in
the another partition
N < 5 Npositivenegative
zero
true
false
A
A
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
15/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
15
Flowchart Examples
A very simple example:
Flowchart for an algorithm which gets twonumbers and prints sum of their value
1. Start
2. Read A and B3. C A + B
4. Print C
5. End
Start
End
Read A , B
C A + B
Print C
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
16/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
16
Flowchart Examples (contd) Flowchart which calculates the real roots of a quadratic equation
and then prints them: ax2+bx+c=0
1. Start2. Read a3. If ( a =0) then go to line 24. Read C and b5. D b2-4ac6. If ( D >0) then
6.1. X1 (-b+D)/2a and X2 (-b-D)/2a6.2. Write X1, X26.3. go to line 9
7. If ( D = 0) then7.1. X -b/(2a)7.2. Write X
7.3. go to line 98. Write no real root9. End
Start
End
Db2-4ac
Read a
a=0
Read c,b
no
Yes
D
X-b/2a
= 0
X1(-b+D)/2aX2(-b-D)/2a
> 0
write x1,x2
A
write no rootA
write X
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
17/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
17
Flowchart Examples (contd)
Flowchart for the problem of printing even numbers
between 9 and 100:Start
End
Write I
I 10
I I+2
I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
18/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
18
Flowchart Examples (contd)
Flowchart for the problem of printing odd numbers less
than a given number. It should also calculate their sumand count.
Put multiple statements in just one symbol
Use lower number of
symbols you can
Start
S0
W
0I 1
Read n
I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
19/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
19
Flowchart Examples (contd)
Flowchart for the problem of determining prime number:
Start
I 2
Read N
write N isprime
B
N=2
no
Yes
R N-I*[N/I]
R=0no
I I+1
A
I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
20/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
20
Flowchart Examples (contd) Quiz? Draw a flowchart which generates first 50 items of the Fibonacci
series.
1, 1, 2, 3, 5, 8,
1. Start2. Write 13. Write 14. I35. F1 16. F2 17. F3 F1+F28. Write F39. I I+110. If (I
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
21/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
21
Arrays in Algorithms
Design an algorithm to convert a decimal
number, n, to binary format: division idea
1. Start
2. Read N
3. R N-2[N/2]
4. Write R
5. N[N/2]
6. If N>0 then go to line 3
7. End
12
12
2
6
6
2
2
2
3
10 01
2
01 The result will be
(0011) instead of(1100)We need a
mechanism to saveintermediateresults
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
22/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
22
Arrays in Algorithms (contd)
Index: A symbol which is used to differentiate
between variables with the same major name
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)
X1 X2
A
A1 A3 A5
A2 A4
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
23/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
23
Arrays in Algorithms (contd) The corrected algorithm:
1. Start
2. Read N3. I14. If N0) then
12.1. J J+112.2. Go to line 9
13. Write A(J)
14. J J-115. If J>0 the go to line 1316. End
This line is only used forbetter understanding,you should not use themin your algorithms
7/30/2019 Sessions 2-3 (Algorithms and Flowchart)
24/24
Sharif University ofTechnology
Introduction to ProgrammingSessions 2,3: Alg. & Flow Chart
24
Arrays in Algorithms (contd)
Flowchart Start
I 1
Read N
write A(J)
BN0
J 1
A
N>0
A
End
B
no
Yes
I I+1
A(J) R
N [N/2]
J J+1
J J-1
no
Yes