Professional Documents
Culture Documents
Flow Charts, Algorithm, Pseudo Code
Flow Charts, Algorithm, Pseudo Code
Pseudo Code
Algorithm
• A set of step-by-step instructions to
accomplish a task.
– An algorithm must have start instruction
– Each instruction must be precise.
– Each instruction must be unambiguous.
– Each instruction must be executed in finite time.
– An algorithm must have stop instruction.
Algorithm Example 1
• Suppose you are given a 1. Start
set of mark sheets 2. Take a mark sheet and
where each mark sheet read the grade.
bears A, B, C or F letter 3. Print the grade
grades.
4. Stop
• Write an algorithm to
read mark sheet and
print the grade that it
contains.
Algorithm Example 2
• Suppose you are given a 1. Start
set of mark sheets 2. Take a mark sheet and
where each mark sheet read the grade.
bears A, B, C or F 3. If grade is A then Print
grades. the grade
Write an algorithm to 4. Stop
read a mark sheet and
print the grade if the
grade is A only.
Algorithm Example 3
• Suppose you are given a 1. Start
set of mark sheets 2. Take a mark sheet and
where each mark sheet read the grade.
bears A, B, C or F 3. If grade is A or B then
grades. Print the grade
Write an algorithm to 4. Stop
read a mark sheet and
print the grade if it is A
or B only.
Algorithm representation
• A pseudo code Pseudocode Example 1
Start
• A flowchart
Take a mark sheet and read the grade.
• Programs statements in Print the grade
a programming Stop
Grade
Grade = A
Grade
Yes No
Grade
STOP
STOP
What is a Flowchart?
START
Display
message “How
many hours did
you work?”
program.
Read Pay Rate
• The figure shown here
is a flowchart for the Multiply Hours
by Pay Rate.
pay-calculating
Store result in
Gross Pay.
END
Basic Flowchart START
Rounded
Rectangle
Symbols Display
message “How
many hours did
you work?”
Symbols Display
message “How
many hours did
you work?”
Multiply Hours
by Pay Rate.
START Store result in
Gross Pay.
Display Gross
Pay
END Terminal
END
Basic Flowchart START
Symbols Display
message “How
many hours did
you work?”
– represented by Display
message “How
parallelograms much do you
Input/Output
Operation
get paid per
– indicate an input or hour?”
Multiply Hours
by Pay Rate.
Display Store result in
Gross Pay.
message “How
Read Hours
many hours Display Gross
Pay
did you work?”
END
Basic Flowchart START
Symbols Display
message “How
many hours did
you work?”
assignment
Multiply
Hours by Pay
Process Rate. Store
Multiply Hours result in
by Pay Rate. Gross Pay.
TERMINAL
Flow line
Output (PRINT)
DIRECT
ACCESS Sequential
STORAGE Access MAGNETIC
DISK
Flowchart/Pseudocode examples
ififAA<<BBthen
then if A < B then
Print
Print“BIG
“BIGB”
B” Start Print “ Big B” If block
else
else
IfIfA>B
A>Bthen
then else
Print
Print“BIG
“BIGA”
A” If A>B then
else
else A,B Print “BIG A”
Print
Print“A=B”
“A=B” else else block
endif
endif Print “A=B”
endif
endif endif
A:B endif
A<B A>B
A=B
A<B A>B
A:B
A=B
A=B
B<C B>C A<C A>C
B:C A:C
Stop
Flowchart/Pseudocode Selection
Start Start
Input A,B,C
Stop
end if
Flowcharts
Start
• Given the input data:
– Student Roll number, student
name, the marks obtained in Roll, name,
5 subjects, each subject m1,m2,m3,m4,m5
having maximum marks 100. Total =
m1+m2+m3+m4+m5
• Draw a flowchart for
Percentage =
the algorithm to: Total/5*100
– Calculate the percentage
marks obtained , and Roll, name, percentage
– Print student’s roll number
and percentage of marks.
Stop
Compute percentages for three students and print
Start their roll numbers, names, and percentage of marks.
Student no=1
Student no 1 2 3
Roll, name, m1,m2,m3,m4,m5
Roll 25 35 27
name A B C
Total = m1+m2+m3+m4+m5 m1 70 80 60
m2 80 70 45
Percentage = Total/5 m3 80 70 90
Student m4 90 70 60
no=Student+1
m5 40 50 30
Roll, name, percentage Total 360 340 285
Percentag 72 68 57
Is e
student
Variables or First Second Third
Yes no <= 3 iteration iteration
Memory iteration
No Stop address
Compute percentages for many students and print
Start their roll numbers, names, and percentage of marks.
Stop
Flowchart Selection
Start
Start
Input A, B
A,B if A > B then
Then block Print “ A is bigger”
end if
end
Yes
Is A>B?
“ A is bigger ”
No
Stop
Flowchart Selection
Start
Start
Input A, B
if A > B then
A,B then block
else
else block Print “ I am watching”
No end if
Is A>B? end
“ I am watching”
Yes
Stop
Flowchart Selection
Start
Start
Input A, B
A,B if A > B then
then block X=A*B
Print X
else
Yes Is No if B=0 then
A>B? Print “ B is zero”
else block else
Is X=A/B
B=0? Print X
X=A*B
No end if
Yes
X=A/B end if
end
X B is zero
Stop
Flowchart Selection
Start
Start
Input A, B
A,B if A > B then
then block X=A*B
Print X
else if B=0 then
Yes Print “ B is zero”
No Is else
A>B? else if block X=A/B
Is Print X
B=0? end if
X=A*B
No end
Yes
X=A/B
X B is zero
Stop
Flowchart Selection (Case)
Start
start
A Input A
If A =1 then
Print “X”
Is A X elseif A =2 then
=1 Print “Y”
elseif A=3 then
start
Is A Print “Z”
Y Input A
=2 else
case A of
Print “W”
1: Print “X”
endif
Is A 2: Print “Y”
Z end
=3 3: Print “Z”
otherwise: Print “W”
endcase
W end
Stop
Flowchart Iteration
Start FOR LOOP Start
for Count = 1 to 5 by 1 do
Range Print Count
Count=1 end for
end
Stop
Advantages of Flowchart
1) Conveys Better Meaning
Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 2
Solution
See Algorithm 8.2 on the next slide.
Algorithm 8.2:Pass/no pass Grade
Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to
33)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “nopass”
End if
2. Return the grade
End
Example 3
Solution
See Algorithm 8.3 on the next slide.
Algorithm 8.3: Letter grade
Letter Grade
Input: One number
1. if (the number is between 90 and 100,
inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89,
inclusive)
then
2.1 Set the grade to “B”
End if
Algorithm 8.3: Letter grade
3. if (the number is between 70 and 79,
inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69,
inclusive)
then
4.1 Set the grade to “D”
End if
Algorithm 8.3: Letter grade
5. If (the number is less than 60)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
Example 4
Solution
SUBALGORITHMS
Concept of a subalgorithm
Algorithm 8.6: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLarger
End while
3. Return Largest
End
Subalgorithm: Find larger
FindLarger
Input: Largest and current integer
1. if (the integer is greater than Largest)
then
1.1 Set Largest to the value of the integer
End if
End