0% found this document useful (0 votes)
3K views

Sessions 2-3 (Algorithms and Flowchart)

its info about algorithm

Uploaded by

Shubham Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Sessions 2-3 (Algorithms and Flowchart)

its info about algorithm

Uploaded by

Shubham Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Programming

Sessions 2 & 3: Introduction to Algorithms and Flowchart


Instructor: Arash Tavakkol [email protected]

Sharif University of Technology Department of Computer Engineering


Spring 2008

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

Basics of Algorithms 4 types of statements :


Conditional Statements
If statements

Calculation Statements
Assignment statements

Explanation Statements
Begin, End

Input/Output Statements

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

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.

An algorithm to calculate even numbers between 10 and 99 Trace Result


Value of I 10 12 14 . . 98 100 Output 10 12 14 . . 98

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Conditional Statements (contd)


Design an algorithm which gets a natural value, n, as its input and calculates odd numbers equal or less than n. Then write them in the standard output:
1. 2. 3. 4. 5. 6. 7. Start Read n I1 Write I II+2 If ( I <= n) then go to line 4 End
Trace Result

Value of I 1 3 5 7 9

Output 1 3 5 7

n 8

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Conditional Statements (contd)


Second type of conditional statements: If <one or more condition> Then <one or more statements> Else <one or more statements> Design an algorithm which generates even numbers between 1000 and 2000 and then prints them in the standard output. It should also print total sum:
1. 2. 3. 4. 5. 6. Start I 1000 and S 0 Write I SS+I II+2 If (I <= 2000) then go to line 3 else go to line 7 7. Write S 8. End

Trace Result

Run 1 2 3 502

Value of I 1000 1002 1004 2002

S 0 1000 2002 750000

Output 1000 1002 1004 750000


6

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Conditional Statements (contd)


Design an algorithm with a natural number, n, as its input which calculates the following formula and writes the result in the standard output: S = + + +1/n 1. Start 2. Read n 3. I 2 and S 0 Trace Result n=8 4. S= S + 1/I Run Value of S Output I 5. I I + 2 1 2 0 6. If (I <= n) then go to line 4 2 4 0.5 else write S in standard output 3 6 0.75 4 8 0.94 7. End
5 10 1.045 1.045
7 Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Conditional Statements (contd)


Loop: A sequence of statements which will be repeated in the algorithm execution. Counter: A variable which defines the number of repeats.
Increment or decrement counter in each execution Check the value of the counter to find out end of execution Usually named: I Most of previous examples
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 8

Conditional Statements (contd)


Determine the value of loop counter in each step of the following algorithm:
1. 2. 3. 4. 5. 6. 7. 8. Start N2 Read i, j and k ti+j+k Write t N N+2 If (N <= 6) then go to line 3 End

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

Final Value of Counter = Initial Value + Changes Inside Loop


Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 9

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

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

10

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? N/2 or N could be 1. Start used but N reduces number 2. Read N of iteration 3. I 2 4. R N-I*[N/I] 5. If ( R = 0) then write N is not prime and go to line 9 6. I I + 1 7. If ( I < N) then go to line 4 8. Write N is prime 9. End This algorithm works for numbers greater than 2
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 11

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

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

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

Assignment and Process


R [N/I] A2 S S+I

Input and Output


Read N
Sharif University of Technology

Write R
13

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Flowchart Symbols (contd)


Conditional Decision Symbol
multiple input or output arrows
negative positive

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

Flowchart Examples (contd)

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

Db2-4ac > 0 X1(-b+D)/2a X2(-b-D)/2a =0 End write X X-b/2a write x1,x2

8. 9.

Write no real root End

write no root

A
16

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Flowchart Examples (contd)


Flowchart for the problem of printing even numbers between 9 and 100:
Start I 10 Write I I I+2 yes

I<=98 no End
Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

17

Flowchart Examples (contd)


Flowchart for the problem of printing odd numbers less than a given number. It should also calculate their sum Start and count.
Put multiple statements in just one symbol Use lower number of A symbols you can
I<=n Yes
no write S write W End Read n S0 W0 I1 write I SS+I ww+1 II+2

A
Sharif University of Technology Introduction to Programming Sessions 2,3: Alg. & Flow Chart 18

Flowchart Examples (contd)


Flowchart for the problem of determining prime number:
Start Read N N=2 no I2 R N-I*[N/I] Yes write N is not prime Yes Yes

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

Flowchart Examples (contd)


Quiz? Draw a flowchart which generates first 50 items of the Fibonacci series.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 1, 1, 2, 3, 5, 8,
Start

11.

10.1. F1 F2 10.2. F2 F3 10.3. go to line 7

Start Write 1 Write 1 I3 F1 1 F2 1 F3 F1+F2 Write F3 I I+1 If (I <= 50) then

write 1 write 1 I3 F1 1 F2 1 F2 F3 F1 F2 F3 F1+F2 write F3 I I+1

Yes I <=50 no End

End

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

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.

Start Read N R N-2[N/2] Write R N[N/2] If N>0 then go to line 3 End

The result will be (0011) instead of (1100) We need a mechanism to save intermediate results
21

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

Arrays in Algorithms (contd)


Index: A symbol which is used to differentiate between variables with the same major name
X1 X2

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

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

22

Arrays in Algorithms (contd)

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

The corrected algorithm:

12.1. J J+1 12.2. Go to line 9

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

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

23

Arrays in Algorithms (contd)


Flowchart
Start Read N I1 Yes N<2I no I I+1 N>0 no write A(J) J J-1 Yes Yes J J+1

A
A(J) R N [N/2]

J1

B
R N-2*[N/2] J>0 no

End

Sharif University of Technology

Introduction to Programming Sessions 2,3: Alg. & Flow Chart

24

You might also like