250 likes | 497 Views
Fundamentals of Computer and programming in C. Rohit Khokher. Algorithm. The word algorithm is the deformed version of the word alkhowarizm : A method used by Abū ʿAbdallāh Muḥammad ibn Mūsā al- Khwārizmī for problem solving.
E N D
Fundamentals of Computer and programming in C RohitKhokher
Algorithm • The word algorithm is the deformed version of the word alkhowarizm: A method used by AbūʿAbdallāhMuḥammadibnMūsā al-Khwārizmī for problem solving. Reference :http://en.wikipedia.org/wiki/Muhammad_ibn_M%C5%ABs%C4%81_al-Khw%C4%81rizm%C4%AB#Arithmetic Al Khwārizmiwas 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 solve quadratic 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.
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.
Algorithm Example 1 • Suppose you are given a set of mark sheets where each mark sheet bears A, B, C or F letter grades. • Write an algorithm to read mark sheet and print the grade that it contains. • Start • Take a mark sheet and read the grade. • Print the grade • Stop
Algorithm Example 2 • Suppose you are given a set of mark sheets where each mark sheet bears A, B, C or F grades. Write an algorithm to read a mark sheet and print the if the grade is A only. • Start • Take a mark sheet and read the grade. • If grade is A then Print the grade • Stop
Algorithm Example 3 • Suppose you are given a set of mark sheets where each mark sheet bears A, B, C or F grades. Write an algorithm to read a mark sheet and print the grade if it is A or B only. • Start • Take a mark sheet and read the grade. • If grade is A or B then Print the grade • Stop
Algorithm representation • A pseudo code • A flowchart • Programs statements in a programming language. Pseudocode Example 1 Start Take a mark sheet and read the grade. Print the grade Stop Pseudocode Example 2 Start Take a mark sheet and read the grade. If grade is A then Print the grade Stop
Flowchart Pseudocode Example 1 START Pseudocode Example 2 START Grade Grade Grade = A Grade Yes No Grade STOP STOP
Flowchart Symbols DECISION PROCESS INPUT TERMINAL Flow line PRINT DIRECT ACCESS STORAGE MAGNETIC DISK Sequential Access FLOWCHART SYMBOLS ARE AVAILABLE IN MSWORD 7
Flowchart/Pseudocode examples if A < B then Print “BIG B” else If A>B then Print “BIG A” else Print “A=B” endif endif if A < B then else endif Start Print “ Big B” If block A,B IfA>B then else endif else block Print “BIG A” Print “A=B” A:B A<B A>B A=B BIG B A=B BIG A Find the biggest of the two numbers A and B Stop
Flowchart/Pseudocode examples Find the largest of three numbers A, B and C. Start 50 30 40 A,B,C A<B A>B A:B A=B A=B B<C B>C B:C A<C A:C A>C BIG C B=C BIG B BIG C A=C BIG A Stop
Flowchart/Pseudocode Selection Start Input A,B,C Start A,B,C If A < B then else end if Print “A<B” If B < C then else end if A<B A>B A:B A>B A=B Print “Big C” A< B A=B B<C B>=C B:C A<=C A:C A>C if B = C then else end if Print “ A=B” B>C A<=C B=C B:C A:C A=C BIG C Print “Big B” BIG A B=C BIG B A=C BIG C Stop
Flowcharts Start • 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 student’s roll number and percentage of marks. Roll, name, m1,m2,m3,m4,m5 Total = m1+m2+m3+m4+m5 Percentage = Total/5 Roll, name, percentage Stop
Compute percentages for three students and print their roll numbers, names, and percentage of marks. Start Student no=1 Roll, name, m1,m2,m3,m4,m5 Total = m1+m2+m3+m4+m5 Percentage = Total/5 Student no=+1 Roll, name, percentage Is student no < 3 Second iteration Third iteration First iteration Initial value Yes Variables or Memory address Stop No
Compute percentages for many students and print their roll numbers, names, and percentage of marks. Start Roll, name, m1,m2,m3,m4,m5 Roll =0 Yes No Total = m1+m2+m3+m4+m5 Percentage = Total/5 Variables or Memory address First iteration Second iteration Third iteration Initial value Roll, name, percentage Stop
Flowchart Selection Start Start Input A, B if A > B then Print “ I am watching” end if end A,B Then block Is A>B? Yes “ I am watching” No Stop
Flowchart Selection Start Start Input A, B if A > B then else Print “ I am watching” end if end A,B then block else block Is A>B? No “ I am watching” Yes Stop
Flowchart Selection Start Start Input A, B if A > B then X=A*B Print X else if B=0 then Print “ B is zero” else X=A/B Print X end if end if end A,B then block Yes Is A>B? No else block Is B=0? X=A*B No Yes X=A/B X B is zero Stop
Flowchart Selection Start Start Input A, B if A > B then X=A*B Print X else if B=0 then Print “ B is zero” else X=A/B Print X end if end A,B then block Yes Is A>B? No else if block Is B=0? X=A*B No Yes X=A/B X B is zero Stop
Flowchart Selection (Case) Start start Input A If A =1 then Print “X” elseif A =20 then Print “Y” elseif A=65 then Print “Z” else Print “W” endif end A Is A = 1 X start Input A case A of 1: Print “X” 2: Print “Y” 3: Print “Z” otherwise: Print “W” endcase end Is A = 20 Y Is A = 65 Z W Stop
Flowchart Iteration FOR LOOP Start for Count = 1 to 5 by 1 do Print Count end for end Start Range Count=1 Start Count = 1 while Count <= 5 do Print Count Count=Count+1 end while end WHILE LOOP Count=+1 Count No Is count=5? Range Yes REPEAT LOOP Start Count = 1 repeat Print Count Count=Count+1 until count > 5 end Stop Range
Flowchart Iteration Start Start Input N for I = 1 to N by 1do Input Roll, Name, m1, m2, m3, m4, m5 Print Roll, Name, m1, m2, m3, m4, m5 end for end N i=1 Roll, Name, m1,m2,m3,m4,m5 Start Input N i= 1 repeat Input Roll, Name, m1, m2, m3, m4, m5 Print Roll, Name, m1, m2, m3, m4, m5 i++ until i > N end i=+1 Roll, Name, m1,m2,m3,m4,m5 Yes Is I < N ? No Stop
Flowchart Iteration Start Count=0 WHILE LOOP Start Count = 0 Input Roll, Name, m1, m2, m3, m4, m5 while Roll <= 5 do Count=Count+1 Input Roll, Name, m1, m2, m3, m4, m5 end while Print Count end Roll, Name, m1,m2,m3,m4,m5 No Count=+1 Is Roll=0? Yes Count Stop