الگوریتم و فلوچارت

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 89

Sadjad University

Advanced Programming in C++-

Algorithm and Flowchart


Reza Shamsaee
Introduction
 Suppose that you want to make
computer to do something…
Sadjad University- Reza Shamsaee

 How should we do this?

 By creating a chain of commands to the


computer that we call it a program

2
Example
Give two numbers and identify the bigger one?
Sadjad University- Reza Shamsaee

What are the types How to identify


of them? them?
Where are they?
Which numbers? ?
?
? ? ?
?
Computer
?
? ?
??
?
? ?
?

3
4
Sadjad University- Reza Shamsaee
Computer Basic Architecture
Example-cont.
 Give two numbers and identify the bigger?
Sadjad University- Reza Shamsaee

 First, the computer capabilities should be understood:


 Inputting: feeding in the data and instruction to the
computer
 Processing: calculations and comparisons
 Arithmetic operations (+,-,*,…)
 Logical operations (and, or, not)
 Relational operations (>, <, ==, !=,…) produce Boolean results
 Storing: dealing with storage unit, CPU, and I/O
 Assignment operations (ab, a=b,…)
 Controlling: timing between different part, branching,…
 Outputting: presenting data to the user in the suitable form

5
ALGORITHMS AND FLOWCHARTS
 A program is a sequence of commands (proper
languages)
Sadjad University- Reza Shamsaee

 A typical programming task can be divided into


two phases:
1-Problem solving phase
 produce an ordered sequence of steps that describe
solution of problem
 this sequence of steps is called an algorithm
2-Implementation phase
 implement the program in some programming language
(translation from steps to a proper language)

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.

 Pseudocode is an artificial and informal


language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.

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

“ algorithm trace table”


 Trace Table
 #Columns ==(number of variables+ output+line number)
 #Rows== (number of steps that the algorithm needs)

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

Suppose that user enters a


value (for example 16) for
M1 variable

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

indicate whether it is passing or failing.


The final grade is calculated as the
average of four marks.

19
Pseudocode & Algorithm
Pseudocode:
 Input a set of 4 marks
Sadjad University- Reza Shamsaee

 Calculate their average by summing and


dividing by 4
 if average is below 50
Print “FAIL”
else
Print “PASS”

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)

 *…(condition for loop)..


 Block
 Go to *

 *…..
 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

your own and send them back (with


in 10 days)

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 (s0)
3 2 0 0 -
3: i=0 4 2 0 0 -
4:while(i<N) 4.1 2 1 0 -

4.1: s=s+(i+1) 4.2 2 1 1 -


4 2 1 1 -
4.2: i=i+1 4.1 2 1+(1+1) 1 -
5:output s 4.2 2 3 2 -
6:stop 4 2 3 2 -
5 2 3 2 3
6 2 3 2 3
44
Example
 Example:
Input N 0: start
number and 1: Input N
Sadjad University- Reza Shamsaee

detect the 2: I=0 (counter)


Maximum of 3: while (I <N)
them 3.1: Input M1
3.2: IF (I==0) MAX=M1
3.3. If(MAX<M1) MAX=M1
3.4: I=I+1
4 : print MAX
5 : stop

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.4 3 1 2 2 - Suppose, N=3,


3 3 1 2 2 - input numbers: 2 ,4, 6
3.1 3 1 4 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

3.1: Fact=Fact*I (Fact*=I) 2 3 1 1 -


3.2: I++ (I=I+1)
4: print Fact 3 3 1 1 -
5; stop 3.1 3 1 1 -
3.2 3 2 1 -
3 3 2 1 -
3.1 3 2 1*2 -
3.2 3 3 2 -
3 3 3 2 -
3.1 3 3 2*3 -
3.2 3 4 6 -
3 3 4 6 -
4 3 4 6 6
5 3 4 6 6
50
Pseudocode & Algorithm
 Example: 0: start
Read a 1: input N
number and
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 - - -

Numbers =1,11,-10,25 3.1 4 2 1 11 -10 - -


3.2 4 3 1 11 -10 - -
3 4 3 1 11 -10 - -
3.1 4 3 1 11 -10 25 -
3.2 4 4 1 11 -10 25 -
3 4 4 1 11 -10 25 -
4 4 0 1 11 -10 25 -
5 4 0 1 11 -10 25 -
54 5.1 4 0 1 11 -10 25 1
Pseudocode & Algorithm
# N I Num(0) Num(1) Num(2) Num(3) output
0: start
1: Input N 5.1 4 0 1 11 -10 25 1
2: I=0 5.2 4 1 1 11 -10 25 1
3:while (I<N)
Sadjad University- Reza Shamsaee

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

Numbers =1,11,-10,25 5.2 4 4 1 11 -10 25 1 11 -10 25


5 4 4 1 11 -10 25 1 11 -10 25
6 4 4 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 - - -

Numbers =1,11,20 3.1 3 2 1 11 20 - -


3.2 3 3 1 11 20 - -
3 3 3 1 11 20 - -
4 3 0 1 11 20 0 -
5 3 0 1 11 20 0 -
5.1 3 0 1 11 20 1 -
5.2 3 1 1 11 20 1 -

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

2.1 Input buf


print 2.2 st1[I]= buf
concatenation 2.3 I++
…//from 1 to 2.3 Similar for st2
of them. 5.I=0
6. while(st1[I]!= “\0”)
6.1 I++
7. J=0
8. while(st2[J]!= “\0”)
8.1 st1[I+J]=st2[J]
8.2 J++
9. st1[I+J]=“\0”
..//A loop is needed here for PRINT st1
13. stop

62
The Flowchart
 (Dictionary) A schematic representation of a sequence
of operations, as in a manufacturing process or
computer program.
Sadjad University- Reza Shamsaee

 (Technical) A graphical representation of the


sequence of operations in an information system or
program. Information system flowcharts show how
data flows from source documents through the
computer to final distribution to users. Program
flowcharts show the sequence of instructions in a
single program or subroutine. Different symbols are
used to draw each type of flowchart.

63
The Flowchart
A Flowchart
 shows logic of an algorithm
Sadjad University- Reza Shamsaee

 emphasizes individual steps and their interconnections


 e.g. control flow from one action to the next

64
Basic Flowchart Symbols
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program


Sadjad University- Reza Shamsaee

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program

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

 Input the length in feet (Lft)


 Calculate the length in cm (Lcm) by multiplying
LFT with 30
 Print length in cm (LCM)

67
Example 2
Algorithm
 Step 1: Input Lft Flowchart
Sadjad University- Reza Shamsaee

 Step 2: Lcm  Lft x 30


 Step 3: Print Lcm START

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 2: AL x W START

 Step 3: Print A
Input
W, L

A  L xW

Print
A

STOP

70
Example 4

 Write an algorithm and draw a flowchart that will


Sadjad University- Reza Shamsaee

calculate the roots of a quadratic equation


ax2  bx  c  0

 Hint: d = sqrt ( b2  4ac ), and the roots are: x1 =


(–b + d)/2a and x2 = (–b – d)/2a

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)

 Step 4: x2  (–b – d) / (2 x a) x1 (–b + d) / (2 x a)


 Step 5: Print x1, x2
X2  (–b – d) / (2 x a)

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

 if A>B is true (if A is greater than B) we


take the action on left
 print the value of A
 if A>B is false (if A is not greater than B)
we take the action on right
 print the value of B

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

> Greater than


< Less than
= (==) Equal to
 Greater than or equal to
 Less than or equal to
 (!=) Not equal to

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

MAX  VALUE1 MAX  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

 may involve further IF–THEN–ELSE statement

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

MAX  N1 [N1>N2, N1>N3]


else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX

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

worked (OVERTIME), hours absent (ABSENT)


and
b) determine the bonus payment (PAYMENT).

85
Example 7
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
Sadjad University- Reza Shamsaee

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10

86
Exampe7-cont.
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT  50
Sadjad University- Reza Shamsaee

else if (OVERTIME–(2/3)*ABSENT > 30) then


PAYMENT  40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT  30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT

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.

You might also like