Download - CMP 101 Set 12 Algorithm and Flowcharts
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
1/23
CMP 101 Fundamentals of
Computer and programming in C
Computer Program Planning
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
2/23
Algorithm
The word algorithm is the deformed version of theword alkhowarizm: A method used byAbAbdallhMuammad ibn Ms al-Khwrizmfor
problem solving.
Al Khwrizmi was a thematician, astronomer and geographer, a scholar in the
House of Wisdom in Baghdad. His contributions had a great impact on language.
"Algebra" is derived from al-jabr, one of the two operations he used to solvequadratic equations.
Algorism and algorithm stem from Algoritmi, the Latin form of his name.His name
is the origin of (Spanish) guarismoand of (Portuguese) algarismo, both meaning
digit.
Reference :http://en.wikipedia.org/wiki/Muhammad_ibn_M%C5%ABs%C4%81_al-
Khw%C4%81rizm%C4%AB#Arithmetic
http://en.wikipedia.org/wiki/Khwarezmhttp://en.wikipedia.org/wiki/Islamic_mathematicshttp://en.wikipedia.org/wiki/Islamic_astronomyhttp://en.wikipedia.org/wiki/Islamic_geographyhttp://en.wikipedia.org/wiki/Scholarhttp://en.wikipedia.org/wiki/House_of_Wisdomhttp://en.wikipedia.org/wiki/Baghdadhttp://en.wikipedia.org/wiki/Quadratic_equationshttp://en.wikipedia.org/wiki/Algorismhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Latinhttp://en.wikipedia.org/wiki/Spanish_languagehttp://en.wikipedia.org/wiki/Portuguese_languagehttp://en.wiktionary.org/wiki/pt:algarismohttp://en.wikipedia.org/wiki/Numerical_digithttp://en.wikipedia.org/wiki/Numerical_digithttp://en.wiktionary.org/wiki/pt:algarismohttp://en.wikipedia.org/wiki/Portuguese_languagehttp://en.wikipedia.org/wiki/Spanish_languagehttp://en.wikipedia.org/wiki/Latinhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Algorismhttp://en.wikipedia.org/wiki/Quadratic_equationshttp://en.wikipedia.org/wiki/Baghdadhttp://en.wikipedia.org/wiki/House_of_Wisdomhttp://en.wikipedia.org/wiki/Scholarhttp://en.wikipedia.org/wiki/Islamic_geographyhttp://en.wikipedia.org/wiki/Islamic_astronomyhttp://en.wikipedia.org/wiki/Islamic_mathematicshttp://en.wikipedia.org/wiki/Khwarezm -
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
3/23
Algorithm
A ser 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.
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
4/23
Algorithm Example 1
Suppose you are given a
set of mark sheets
where each mark sheet
bears A, B, C or F lettergrades.
Write an algorithm to
read mark sheet and
print the grade that it
contains.
1. Start
2. Take a mark sheet and
read the grade.
3. Print the grade
4. Stop
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
5/23
Algorithm Example 2
Suppose you are given a
set of mark sheets
where each mark sheet
bears A, B, C or Fgrades.
Write an algorithm to read
a mark sheet and print
the if the grade is A
only.
1. Start
2. Take a mark sheet and
read the grade.
3. If grade is A then Printthe grade
4. Stop
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
6/23
Algorithm Example 3
Suppose you are given a
set of mark sheets
where each mark sheet
bears A, B, C or Fgrades.
Write an algorithm to read
a mark sheet and print
the grade if it is A or Bonly.
1. Start
2. Take a mark sheet and
read the grade.
3. If grade is A or B thenPrint the grade
4. Stop
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
7/23
Algorithm representation
A pseudo code
A flowchart
Programs statements in
a programminglanguage.
Pseudocode Example 1Start
Take a mark sheet and read the grade.
Print the grade
Stop
Pseudocode Example 2
Start
Take a mark sheet and read the
grade.
Ifgrade is A then Print the grade
Stop
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
8/23
FlowchartPseudocode Example 1
START
Grade
Grade
Pseudocode Example 2
START
Grade
Grade = A
Grade
NoYes
STOP
STOP
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
9/23
Flowchart Symbols
INPUT DECISION
TERMINAL
Flow line
PROCESS
PRINT
DIRECT
ACCESSSTORAGE
SequentialAccess MAGNETIC
DISK
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
10/23
Flowchart/Pseudocode examples
Start
A:B
A,B
BIG ABIG B A=B
Stop
AB
A=B
if A < B thenPrint BIG B
else
If A>B then
Print BIG A
else
Print A=Bendif
endif
ifA < B
then
else
endif
IfA>B then
else
endif
If block
else block
Print Big B
Print BIG A
Print A=B
Find the biggest of the
two numbers A and B
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
11/23
Flowchart/Pseudocode examples
A:B
Start
B:C A:C
BIG C
A,B,C
A>BAC
BIG C
A
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
12/23
Flowchart/Pseudocode Selection
If A < B then
else
end if
A:B
Start
B:C A:C
BIG C
A,B,C
A>BA=C
BIG C
AC
A< B
A:C
A>B
A=CA
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
13/23
Flowcharts
Given the input data: Student id number, student
name, the marks obtained in
5 subjects, each subject
having maximum marks 100.
Draw a flowchart for
the algorithm to: Calculate the percentage
marks obtained , and Print students roll number
and percentage of marks.
Start
Roll, name,
m1,m2,m3,m4,m5
Total =m1+m2+m3+m4+m5
Percentage = Total/5
Roll, name, percentage
Stop
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
14/23
Start
Roll, name, m1,m2,m3,m4,m5
Total = m1+m2+m3+m4+m5
Percentage = Total/5
Roll, name, percentage
Stop
Student no=1
Student
no=+1
Is
student
no < 3Yes
No
Compute percentages for three students and print
their roll numbers, names, and percentage of marks.
Student no
Roll
name
m1
m2
m3
m4
m5
TotalPercentage
0
0
0
0
0
0
0
00
Initialvalue
Variables orMemory
address
1
25
A
70
80
80
90
40
36072
Firstiteration
2
25
A
70
80
80
90
40
36072
2
35
B
80
70
70
70
50
34068
Seconditeration
3
35
B
80
70
70
70
50
34068
Thirditeration
3
27
C
60
45
90
60
30
28557
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
15/23
Start
Roll, name, m1,m2,m3,m4,m5
Total = m1+m2+m3+m4+m5
Percentage = Total/5
Roll, name, percentage
Stop
Roll =0Yes
Compute percentages for many students and print
their roll numbers, names, and percentage of marks.
Rollname
m1
m2
m3
m4
m5
Total
Percentage
0
0
0
0
0
0
0
0
Initialvalue
Variables orMemoryaddress
Firstiteration
25A
70
80
80
90
40
360
72
35B
80
70
70
70
50
340
68
Seconditeration
Thirditeration
No
0
0
0
0
0
0
340
68
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
16/23
Flowchart Selection
A,B
Is A>B?
Stop
I am watching
Start
StartInput A, BifA > B then
Print I am watchingend if
end
Then block
Yes
No
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
17/23
Flowchart Selection
A,B
Is A>B?
Stop
I am watching
Start
StartInput A, BifA > B then
else
Print I am watching
end if
end
then block
No
Yes
else block
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
18/23
Flowchart Selection
A,B
Is
A>B?
Stop
Start
StartInput A, BifA > B then
X=A*BPrintX
else
ifB=0 thenPrint B is zero
elseX=A/B
PrintXend if
end if
end
then block
No Yes
else block
X
X=A*B
X=A/B
Is
B=0?
No
B is zero
Yes
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
19/23
Flowchart Selection
A,B
Is
A>B?
Stop
Start
StartInput A, BifA > B then
X=A*BPrintX
else ifB=0 then
Print B is zeroelseX=A/BPrintX
end if
end
then block
No Yes
else if block
X
X=A*B
X=A/B
Is
B=0?
No
B is zero
Yes
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
20/23
Flowchart Selection (Case)
A
Is A
= 1
Stop
Start
X
Is A
= 20
Is A= 65
Y
Z
W
start
Input A
case A of
1: Print X
2: Print Y
3: Print Zotherwise: Print W
endcase
end
start
Input A
If A =1 then
PrintX
elseifA =20 then
Print Y
elseifA=65 then
Print Z
else
Print W
endif
end
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
21/23
Flowchart Iteration
Is
count=5?
Stop
Start Start
forCount = 1 to 5 by1 doPrintCount
end for
end
No
CountCount=+1
Count=1
Start
Count = 1
while Count 5
end
REPEAT LOOP
Range
Range
Range
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
22/23
Flowchart Iteration
Is I < N ?
Stop
Start
No
i=+1
i=1
Start
InputN
for I = 1 to N by 1do
InputRoll, Name, m1, m2, m3, m4, m5
PrintRoll, Name, m1, m2, m3, m4, m5
end for
end
Yes
Roll, Name,m1,m2,m3,m4,m5
Roll, Name,
m1,m2,m3,m4,m5
N
Start
InputN
i = 1
repeat
InputRoll, Name, m1, m2, m3, m4, m5PrintRoll, Name, m1, m2, m3, m4, m5
i++
until i > N
end
-
8/4/2019 CMP 101 Set 12 Algorithm and Flowcharts
23/23
Flowchart Iteration
Is Roll=0?
Stop
Start
No
Count=+1
Count=0
Start
Count = 0InputRoll, Name, m1, m2, m3, m4, m5
while Roll