Professional Documents
Culture Documents
الگوریتم و فلوچارت
الگوریتم و فلوچارت
الگوریتم و فلوچارت
2
Example
Give two numbers and identify the bigger one?
Sadjad University- Reza Shamsaee
3
4
Sadjad University- Reza Shamsaee
Computer Basic Architecture
Example-cont.
Give two numbers and identify the bigger?
Sadjad University- Reza Shamsaee
5
ALGORITHMS AND FLOWCHARTS
A program is a sequence of commands (proper
languages)
Sadjad University- Reza Shamsaee
6
Steps in Problem Solving
First produce a general algorithm (one can use
pseudocode)
Refine the algorithm successively to get step
Sadjad University- Reza Shamsaee
by step detailed algorithm that is very close
to a computer language.
7
Pseudocode & Algorithm
Example: Write an algorithm to
determine an input number is positive,
Sadjad University- Reza Shamsaee
negative or zero?
8
Pseudocode & Algorithm
Pseudocode:
Input a number
if number is below 0
Sadjad University- Reza Shamsaee
Print “Negative”
else if number is greater than 0
Print “Positive”
else Print “Zero”
9
Pseudocode & Algorithm
Detailed Algorithm (it should be close to c++
commands)
An input number in
Sadjad University- Reza Shamsaee
computer should
step 0 : start have a name to be
mentioned
step 1 : input M1
step 2 : if (M1<0)
print “Negative”
else if (M1>0) A variable is a named
print “Positive” unit of data that may be
else assigned a value. If the
value is modified, the
print “Zero” name does not change.
step 3: stop
10
Pseudocode & Algorithm
But does our algorithm right?
There is a way to understand it
Sadjad University- Reza Shamsaee
11
Pseudocode & Algorithm
0: start
1: input M1
# M1 Output 2: if (M1<0)
Sadjad University- Reza Shamsaee
0 - - print “Negative”
else if (M1>0)
print “Positive”
else
print “Zero”
3: stop
12
Pseudocode & Algorithm
0: start
1: input M1
# M1 Output 2: if (M1<0)
Sadjad University- Reza Shamsaee
0 - - print “Negative”
else if (M1>0)
1 16 -
print “Positive”
else
print “Zero”
3: stop
13
Pseudocode & Algorithm
0: start
1: input M1
# M1 Output 2: if (M1<0)
Sadjad University- Reza Shamsaee
0 - - print “Negative”
else if (M1>0)
1 16 -
print “Positive”
2 16 Positive else
print “Zero”
3: stop
14
Pseudocode & Algorithm
0: start
1: input M1
# M1 Output 2: if (M1<0)
Sadjad University- Reza Shamsaee
0 - - print “Negative”
else if (M1>0)
1 16 -
print “Positive”
2 16 Positive else
3 16 Positive print “Zero”
3: stop
15
Select
If ( statement) [then]
Block -If
Sadjad University- Reza Shamsaee
else
Block- else
16
Pseudocode & Algorithm
Example: Give two numbers and identify the
bigger?
Sadjad University- Reza Shamsaee
0: Start
1: Input A
2: Input B
3: If(A>B) print A else print B
4: Stop
17
Pseudocode & Algorithm
Example: Give two numbers and identify the
bigger? # A B Output
Sadjad University- Reza Shamsaee
0: Start 0 - - -
1: Input A
1 11 - -
2: Input B
3: If(A>B) print A else print B
2 11 3 -
4: Stop 3 11 3 11
4 11 3 11
18
Pseudocode & Algorithm
Example: Write an algorithm to
determine a student’s final grade and
Sadjad University- Reza Shamsaee
19
Pseudocode & Algorithm
Pseudocode:
Input a set of 4 marks
Sadjad University- Reza Shamsaee
20
Pseudocode & Algorithm
Detailed Algorithm
0: start
Sadjad University- Reza Shamsaee
1: Input M1,M2,M3,M4
2: GRADE (M1+M2+M3+M4)/4 (= == )
3: if (GRADE < 50)
Print “FAIL”
else
Print “PASS”
4: stop
21
Iteration (Loop)
*…(Infinite Loop)..
Block
Sadjad University- Reza Shamsaee
Go to * (goto == jump==branch)
*…..
Block
(condition for loop) Go to *
22
Pseudocode & Algorithm
Example: Write an algorithm to give 3
inputs and write them.
Sadjad University- Reza Shamsaee
0 : start
1 : I=0
2 : if (I <3)
2.1 : Input M1
If Block
2.2 : print M1
2.3 : I=I+1
2.4 : goto 2
3 : stop
23
Pseudocode & Algorithm
# I M1 Output
0 : start
0 - - -
1 : I=0
1 0 - -
2 : if (I <2)
2 0 - -
Sadjad University- Reza Shamsaee
2.1: Input M1
2.1 0 13 -
2.2 0 13 13
2.2: print M1
2.3 1 13 13 2.3: I=I+1
2.4 1 13 13 2.4: goto 2
2 1 13 13 3: stop
2.1 1 15 13
2.2 1 15 1315
2.3
2.4
2
2
15
15
1315
1315
Suppose,
2 2 15 1315 input
3 2 15 1315
numbers:
13,15
That is not in
our mind!
24
Pseudocode & Algorithm
Example: Write an algorithm to find sum
of N input.
0 : start
Sadjad University- Reza Shamsaee
1 : Input N
2 : I=0 (counter), Sum=0
3 : if (I <N)
3.1 : Input M1
If Block
3.2 : Sum=Sum+M1
3.3 : I=I+1
3.4 : goto 3
4 : print Sum
5 : stop
25
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - - 1: Input N
2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
3: if (I <N)
3.1: Input M1
3.2: Sum=Sum+M1
3.3: I=I+1
3.4: goto 3
4 : print Sum
5 : stop
Suppose, N=2,
input numbers: 13,15
26
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
3: if (I <N)
3.1: Input M1
3.2: Sum=Sum+M1
3.3: I=I+1
3.4: goto 3
4 : print Sum
5 : stop
27
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3.1: Input M1
3.2: Sum=Sum+M1
3.3: I=I+1
3.4: goto 3
4 : print Sum
5 : stop
28
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.2: Sum=Sum+M1
3.3: I=I+1
3.4: goto 3
4 : print Sum
5 : stop
29
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.3: I=I+1
3.4: goto 3
4 : print Sum
5 : stop
30
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.4: goto 3
4 : print Sum
5 : stop
31
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
4 : print Sum
5 : stop
32
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
5 : stop
33
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
34
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
35
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
3.2 2 1 15 28 -
36
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
3.2 2 1 15 28 -
3.3 2 2 15 28 -
37
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
3.2 2 1 15 28 -
3.3 2 2 15 28 -
3.4 2 2 15 28 -
38
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
3.2 2 1 15 28 -
3.3 2 2 15 28 -
3.4 2 2 15 28 -
3 2 2 15 28 -
39
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
3.2 2 1 15 28 -
3.3 2 2 15 28 -
3.4 2 2 15 28 -
3 2 2 15 28 -
4 2 2 15 28 28
40
Pseudocode & Algorithm
# N I M1 Sum Output 0: start
0 - - - - 1: Input N
1 2 - - - - 2: I=0 (counter), Sum=0
Sadjad University- Reza Shamsaee
2 2 0 - 0 - 3: if (I <N)
3 2 0 - 0 - 3.1: Input M1
3.1 2 0 13 0 - 3.2: Sum=Sum+M1
3.2 2 0 13 13 - 3.3: I=I+1
3.3 2 1 13 13 - 3.4: goto 3
3.4 2 1 13 13 - 4 : print Sum
3 2 1 13 13 - 5 : stop
3.1 2 1 15 13 -
3.2 2 1 15 28 -
3.3 2 2 15 28 -
3.4 2 2 15 28 -
3 2 2 15 28 -
4 2 2 15 28 28
5 2 2 15 28 28
41
Questions
Look to the pdf file of home
works that you should do it by
Sadjad University- Reza Shamsaee
42
Pseudocode & Algorithm
Example: Re-Write the algorithm with While -
loop
0 : start
Sadjad University- Reza Shamsaee
1 : Input N
2 : I=0 (counter), Sum=0
3 : while(I <N)
3.1 : Input M1
3.2 : Sum=Sum+M1
3.3 : I=I+1
4 : print Sum
5 : stop
43
Pseudocode & Algorithm
Example: Write an algorithm to find sum of N
consequent number from 1 (s=1+..+N) N=2
Sadjad University- Reza Shamsaee
# N s i output
0: start 0 - - - -
1: Input N 1 2 - - -
2 2 0 - -
2: s=0 (s0)
3 2 0 0 -
3: i=0 4 2 0 0 -
4:while(i<N) 4.1 2 1 0 -
45
Example -MAX
# N I M1 MAX Output 0: start
1: Input N
0 - - - - - 2: I=0 (counter)
3: while (I <N)
1 3 - - - -
Sadjad University- Reza Shamsaee
3.1: Input M1
2 3 0 - - - 3.2: If (I==0) MAX=M1
3.3. If(MAX<M1) MAX=M1
3 3 0 - - - 3.4: I=I+1
4 : print MAX
3.1 3 0 2 - -
5 : stop
3.2 3 0 2 2 -
3.3 3 0 2 2 -
3.2 3 1 4 2 -
3.3 3 1 4 4 -
3.4 3 2 4 4 -
3 3 2 4 4 -
46
Example -MAX
# N I M1 MAX Output 0: start
1: Input N
3 3 2 4 4 - 2: I=0 (counter)
3: while (I <N)
3.1 3 2 6 4 -
Sadjad University- Reza Shamsaee
3.1: Input M1
3.2 3 2 6 4 - 3.2: If (I==0) MAX=M1
3.3. If(MAX<M1) MAX=M1
3.3 3 2 6 6 - 3.4: I=I+1
4 : print MAX
3.4 3 3 6 6 -
5 : stop
3 3 3 6 6 -
4 3 3 6 6 6
Suppose, N=3,
input numbers: 2 ,4, 6
47
Pseudocode & Algorithm
Example: 0: start
Read 3 1: input A,B,C
numbers and
Sadjad University- Reza Shamsaee
2: if ((A>=B)&&(B>=C) print(A,B,C)
print them 3: if ((A>=C)&&(C>=B) print(A,C,B)
descending. 4: if ((B>=A)&&(A>=C) print(B,A,C)
5: if ((B>=C)&&(C>=A) print(B,C,A)
6: if ((C>=A)&&(A>=B) print(C,A,B)
7: if ((C>=B)&&(B>=A) print(C,B,A)
8: stop
48
Pseudocode & Algorithm
Example: 0: start
Read a 1: input A
number and
Sadjad University- Reza Shamsaee
2: I=1 , Fact=1
print its 3: while (I<=A)
factorial. 3.1: Fact=Fact*I (Fact*=I)
3.2: I++ (I=I+1)
4: print Fact
5; stop
49
Pseudocode & Algorithm
0: start # A I Fact output
1: input A
0 - - - -
2: I=1 , Fact=1
3: while (I<=A) 1 3 - - -
Sadjad University- Reza Shamsaee
2: I=0, fib1=1,fib2=1
print 3: while (I<N)
fibonacci. 3.1: if(I==0) print fib1
3.2: else if(I==1) print fib2
3.2.1: else
3.2.1.1: fib= fib1+fib2
3.2.1.2: print fib
3.2.1.3: fib1=fib2
3.2.1.4: fib2=fib
3.3: I++
51
4: stop
Pseudocode & Algorithm
# N I fib1 fib2 fib output
0: start 0 - - - - -
1: input N 1 3 - - - - -
Sadjad University- Reza Shamsaee
2: I=0, fib1=1,fib2=1 2 3 0 1 1 - -
3: while (I<N)
3 3 0 1 1 - -
3.1: if(I==0) print fib1
3.1 3 0 1 1 - 1
3.2: else if(I==1) print fib2
3.2.1: else 3.3 3 1 1 1 - 1
3.2.1.1: fib= fib1+fib2 3 3 1 1 1 - 1
3.2.1.2: print fib 3.1 3 1 1 1 - 1
3.2.1.3: fib1=fib2
3.2 3 1 1 1 - 11
3.2.1.4: fib2=fib
3.3 3 2 1 1 - 11
3.3: I++
4: stop 3 3 2 1 1 - 11
3.1 3 2 1 1 - 11
3.2 3 2 1 1 - 11
3.2.1.1 3 2 1 1 2 11
3.2.1.2 3 2 1 1 2 112
3.2.1.3 3 2 1 1 2 112
3.2.1.4 3 2 1 2 2 112
3.3 3 3 1 2 2 112
52 3
Pseudocode & Algorithm
(Indexed variable= array)
Example: 0: start
Read N and 1: Input N
input all
Sadjad University- Reza Shamsaee
2: I=0
numbers and 3:while (I<N)
then print 3.1: Input Num(I)
them all.
3.2: I++
//PROCESS
4: I=0
5:while (I<N)
5.1: Output Num(I)
5.2: I++
6 : stop
53
Pseudocode & Algorithm
# N I Num(0) Num(1) Num(2) Num(3) output
0: start
1: Input N 0 - - - - - - -
2: I=0 1 4 - - - - - -
3:while (I<N)
Sadjad University- Reza Shamsaee
2 4 0 - - - - -
3.1: Input Num[I]
3 4 0 - - - - -
3.2: I++
3.1 4 0 1 - - - -
//PROCESS
4: I=0 3.2 4 1 1 - - - -
5:while (I<N) 3 4 1 1 - - - -
5.1: Output Num[I] 3.1 4 1 1 11 - - -
5.2: I++
3.2 4 2 1 11 - - -
6 : stop
3 4 2 1 11 - - -
5 4 1 1 11 -10 25 1
3.1: Input Num[I]
5.1 4 1 1 11 -10 25 1 11
3.2: I++
5.2 4 2 1 11 -10 25 1 11
//PROCESS
4: I=0 5 4 2 1 11 -10 25 1 11
5:while (I<N) 5.1 4 2 1 11 -10 25 1 11 -10
5.1: Output Num[I] 5.2 4 3 1 11 -10 25 1 11 -10
5.2: I++
5 4 3 1 11 -10 25 1 11 -10
6 : stop
5.1 4 3 1 11 -10 25 1 11 -10 25
55
Pseudocode & Algorithm
Example: 0: start
Read N and 1: Input N
input all
Sadjad University- Reza Shamsaee
2: I=0
numbers and 3: while (I<N)
then print 3.1 Input dat[I]
SUM of them
3.2.I++
all.
4: I=0 , Sum=0
5: while (I<N)
5.1 Sum=dat[I]+Sum
5.2.I++
6. Print Sum
7. stop
56
Pseudocode & Algorithm
# N I dat(0) dat(1) dat(2) Sum output
0: start
1: Input N 0 - - - - - - -
2: I=0 1 3 - - - - - -
3: while (I<N)
Sadjad University- Reza Shamsaee
2 3 0 - - - - -
3.1 Input dat[I]
3 3 0 - - - - -
3.2.I++
3.1 3 0 1 - - - -
4: I=0 , Sum=0
5: while (I<N) 3.2 3 1 1 - - - -
5.1 Sum=dat[I]+Sum 3 3 1 1 - - - -
5.2.I++ 3.1 3 1 1 11 - - -
6. Print Sum
3.2 3 2 1 11 - - -
7. stop
3 3 2 1 11 - - -
57
Pseudocode & Algorithm
# N I dat(0) dat(1) dat(2) Sum output
0: start
1: Input N 5.2 3 1 1 11 20 1 -
2: I=0 5 3 1 1 11 20 1 -
3: while (I<N)
Sadjad University- Reza Shamsaee
5.1 3 1 1 11 20 12
3.1 Input dat[I]
5.2 3 2 1 11 20 12 -
3.2.I++
5 3 2 1 11 20 12 -
4: I=0 , Sum=0
5: while (I<N) 5.1 3 2 1 11 20 32 -
5.1 Sum=dat[I]+Sum 5.2 3 3 1 11 20 32 -
5.2.I++ 5 3 3 1 11 20 32 -
6. Print Sum
6 3 3 1 11 20 32 32
7. stop
7 3 3 1 11 20 32 32
Numbers =1,11,20
58
Pseudocode & Algorithm
Example: 0: start
Read N and 1: Input N
input all
Sadjad University- Reza Shamsaee
2: I=0
numbers and 3: while (I<N)
then print the 3.1 Input dat[I]
maximum of
3.2.I++
them all.
4: I=1 , Max=dat[0]
5: while (I<N)
5.1 if(Max<dat[I]) Max=dat[I]
5.2.I++
6. Print Max
7. stop
59
Pseudocode & Algorithm
# N I dat(0) dat(1) dat(2) Max output
0: start
1: Input N 0 - - - - - - -
2: I=0 1 3 - - - - - -
3: while (I<N)
Sadjad University- Reza Shamsaee
2 3 0 - - - - -
3.1 Input dat[I]
3 3 0 - - - - -
3.2.I++
3.1 3 0 1 - - - -
4: I=1 , Max=dat[0]
5: while (I<N) 3.2 3 1 1 - - - -
5.1 if(Max<dat[I]) 3 3 1 1 - - - -
Max=dat[I] 3.1 3 1 1 11 - - -
5.2.I++
3.2 3 2 1 11 - - -
6. Print Max
3 3 2 1 11 - - -
7. stop
3.1 3 2 1 11 20 - -
Numbers =1,11,20 3.2 3 3 1 11 20 - -
3 3 3 1 11 20 - -
4 3 1 1 11 20 1 -
5 3 1 1 11 20 1 -
5.1 3 1 1 11 20 11 -
5.2 3 2 1 11 20 11 -
60
Pseudocode & Algorithm
# N I dat(0) dat(1) dat(2) Max output
0: start
1: Input N 5.2 3 2 1 11 20 11 -
2: I=0 5 3 2 1 11 20 11 -
3: while (I<N)
Sadjad University- Reza Shamsaee
5.1 3 2 1 11 20 20 -
3.1 Input dat[I]
5.2 3 3 1 11 20 20 -
3.2.I++
5 3 3 1 11 20 20 -
4: I=1 , Max=dat[0]
5: while (I<N) 6 3 3 1 11 20 20 20
5.1 if(Max<dat[I]) 7 3 3 1 11 20 20 20
Max=dat[I]
5.2.I++
6. Print Max
7. stop
Numbers =1,11,20
61
Pseudocode & Algorithm
Example: 0: start
Read two 1: I=0 , buf=1
2: while(buf!= “\0”)
strings and
Sadjad University- Reza Shamsaee
62
The Flowchart
(Dictionary) A schematic representation of a sequence
of operations, as in a manufacturing process or
computer program.
Sadjad University- Reza Shamsaee
63
The Flowchart
A Flowchart
shows logic of an algorithm
Sadjad University- Reza Shamsaee
64
Basic Flowchart Symbols
Name Symbol Use in Flowchart
65
Example
Sadjad University- Reza Shamsaee
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0
PRINT PRINT
“PASS” “FAIL”
STOP
66
Example 2
Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
Sadjad University- Reza Shamsaee
67
Example 2
Algorithm
Step 1: Input Lft Flowchart
Sadjad University- Reza Shamsaee
Input
Lft
Lcm Lft x 30
Print
Lcm
STOP
68
Example 3
Write an algorithm and draw a flowchart
that will read the two sides of a rectangle
and calculate its area.
Sadjad University- Reza Shamsaee
Pseudocode
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L with W
Print A
69
Example 3
Algorithm
Step 1: Input W,L
Sadjad University- Reza Shamsaee
Step 3: Print A
Input
W, L
A L xW
Print
A
STOP
70
Example 4
71
Example 4
Pseudocode:
Input the coefficients (a, b, c) of the quadratic
equation
Sadjad University- Reza Shamsaee
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
72
Example 4
START
Sadjad University- Reza Shamsaee
Algorithm:
Input
Step 1: Input a, b, c a, b, c
Step 2: d sqrt ( b b 4 a c )
Step 3: x1 (–b + d) / (2 x a) d sqrt(b x b – 4 x a x c)
Print
x1 ,x2
STOP
73
DECISION STRUCTURES
The expression A>B is a logical expression
it describes a condition we want to test
Sadjad University- Reza Shamsaee
74
DECISION STRUCTURES
Sadjad University- Reza Shamsaee
Y N
is
A>B
Print Print
A B
75
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
Sadjad University- Reza Shamsaee
true alternative
else
false alternative
endif
76
IF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as follows:
If A>B then
print A
Sadjad University- Reza Shamsaee
else
print B
endif
Y N
is
A>B
Print Print
A B
77
Relational Operators
Relational Operators
Operator Description
Sadjad University- Reza Shamsaee
78
Example 5
Write an algorithm that reads two values, determines
the largest value and prints the largest value with an
identifying message.
Sadjad University- Reza Shamsaee
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
79
Example 5
START
Sadjad University- Reza Shamsaee
Input
VALUE1,VALUE2
Y is
N
VALUE1>VALUE2
Print
“The largest value is”, MAX
STOP
80
NESTED IFS
One of the alternatives within an IF–THEN–ELSE
statement
Sadjad University- Reza Shamsaee
81
Example 6
Write an algorithm that reads three numbers and
prints the value of the largest number.
Sadjad University- Reza Shamsaee
82
Example 6
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
Sadjad University- Reza Shamsaee
83
Example 6
Flowchart: Draw the flowchart of the above
Algorithm.
Sadjad University- Reza Shamsaee
84
Example 7
Write and algorithm and draw a flowchart to
a) read an employee name (NAME), overtime hours
Sadjad University- Reza Shamsaee
85
Example 7
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
Sadjad University- Reza Shamsaee
86
Exampe7-cont.
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT 50
Sadjad University- Reza Shamsaee
87
Example 7
Flowchart: Draw the flowchart of the above
algorithm?
Sadjad University- Reza Shamsaee
88
Sadjad University- Reza Shamsaee
89
End of Alg.&Flow.