Algorithms and Flowcharts

117,845 views

Published on

This presentation covers all the basic fundamentals of Algorithms & Flowcharts.
( Included examples )

Published in: Education
11 Comments
95 Likes
Statistics
Notes
No Downloads
Views
Total views
117,845
On SlideShare
0
From Embeds
0
Number of Embeds
162
Actions
Shares
0
Downloads
3,506
Comments
11
Likes
95
Embeds 0
No embeds

No notes for slide

Algorithms and Flowcharts

  1. 1. COMPUTER SCIENCECOVERED BASICS ABOUTALGORITHMS AND FLOWCHARTS
  2. 2. ALGORITHMS AND FLOWCHARTS A typical programming task can be divided intotwo phases: Problem solving phase produce an ordered sequence of steps that describesolution of problem this sequence of steps is called an algorithm Implementation phase implement the program in some programminglanguage
  3. 3. STEPS IN PROBLEM SOLVING First produce a general algorithm (one canuse pseudo code) Refine the algorithm successively to getstep by step detailed algorithm that is veryclose to a computer language. Pseudo code is an artificial and informallanguage that helps programmers developalgorithms. Pseudo code is very similar toeveryday English.
  4. 4. PSEUDOCODE & ALGORITHM Example 1: Write an algorithm to determine astudent’s final grade and indicate whether it ispassing or failing. The final grade is calculated asthe average of four marks.
  5. 5. PSEUDOCODE & ALGORITHMPseudo code: Input a set of 4 marks Calculate their average by summing anddividing by 4 if average is below 50Print “FAIL”elsePrint “PASS”
  6. 6. PSEUDOCODE & ALGORITHM Detailed AlgorithmStep 1: Input M1,M2,M3,M4Step 2: GRADE (M1+M2+M3+M4)/4Step 3: if (GRADE < 50) thenPrint “FAIL”elsePrint “PASS”endif
  7. 7. THE FLOWCHART (Dictionary) A schematic representation of asequence of operations, as in a manufacturingprocess or computer program. (Technical) A graphical representation of thesequence of operations in an information system orprogram. Information system flowcharts show howdata flows from source documents through thecomputer to final distribution to users. Programflowcharts show the sequence of instructions in asingle program or subroutine. Different symbols areused to draw each type of flowchart.
  8. 8. THE FLOWCHARTA Flowchart shows logic of an algorithm emphasizes individual steps and their interconnections e.g. control flow from one action to the next
  9. 9. FLOWCHART SYMBOLSOvalParallelogramRectangleDiamondHybridName Symbol Use in FlowchartDenotes the beginning or end of the programDenotes an input operationDenotes an output operationDenotes a decision (or branch) to be made.The program should continue along one oftwo routes. (e.g. IF/THEN/ELSE)Denotes a process to be carried oute.g. addition, subtraction, division etc.Flow line Denotes the direction of logic flow in the programBasic
  10. 10. EXAMPLE 1PRINT“PASS”Step 1: Input M1,M2,M3,M4Step 2: GRADE (M1+M2+M3+M4)/4Step 3: if (GRADE <50) thenPrint “FAIL”elsePrint “PASS”endifSTARTInputM1,M2,M3,M4GRADE (M1+M2+M3+M4)/4ISGRADE<50PRINT“FAIL”STOPYN
  11. 11. EXAMPLE 2 Write an algorithm and draw a flowchart to convertthe length in feet to centimeter.Pseudo code: Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFTwith 30 Print length in cm (LCM)
  12. 12. Algorithm Step 1: Input Lft Step 2: Lcm Lft x 30 Step 3: Print LcmSTARTInputLftLcm Lft x 30PrintLcmSTOPFlowchart
  13. 13. EXAMPLE 3Write an algorithm and draw a flowchartthat will read the two sides of a rectangleand calculate its area.Pseudocode Input the width (W) and Length (L) of arectangle Calculate the area (A) by multiplying L withW Print A
  14. 14. Algorithm Step 1: Input W,L Step 2: A L x W Step 3: Print ASTARTInputW, LA L x WPrintASTOP
  15. 15. EXAMPLE 4 Write an algorithm and draw a flowchart thatwill calculate the roots of a quadratic equation Hint: d = sqrt ( ), and the roots are:x1 = (–b + d)/2a and x2 = (–b – d)/2a20ax bx c24b ac
  16. 16. Pseudo code: Input the coefficients (a, b, c) of the quadraticequation Calculate d Calculate x1 Calculate x2 Print x1 and x2
  17. 17.  Algorithm: Step 1: Input a, b, c Step 2: d sqrt ( ) Step 3: x1 (–b + d) / (2 x a) Step 4: x2 (–b – d) / (2 x a) Step 5: Print x1, x24b b a cSTARTInputa, b, cd sqrt(b x b – 4 x a x c)Printx1 ,x2STOPx1 (–b + d) / (2 x a)X2 (–b – d) / (2 x a)
  18. 18. DECISION STRUCTURES The expression A>B is a logical expression it describes a condition we want to test if A>B is true (if A is greater than B) we takethe action on left print the value of A if A>B is false (if A is not greater than B) wetake the action on right print the value of B
  19. 19. DECISION STRUCTURESisA>BPrintBPrintAY N
  20. 20. IF–THEN–ELSE STRUCTURE The structure is as follows:If condition thentrue alternativeelsefalse alternativeendif
  21. 21. IF–THEN–ELSE STRUCTURE The algorithm for the flowchart is as follows:If A>B thenprint Aelseprint BendifisA>BPrintBPrintAY N
  22. 22. RELATIONAL OPERATORSRelational OperatorsOperator Description> Greater than< Less than= Equal toGreater than or equal toLess than or equal toNot equal to
  23. 23. EXAMPLE 5 Write an algorithm that reads twovalues, determines the largest value and prints thelargest value with an identifying message.ALGORITHMStep 1: Input VALUE1, VALUE2Step 2: if (VALUE1 > VALUE2) thenMAX VALUE1elseMAX VALUE2endifStep 3: Print “The largest value is”, MAX
  24. 24. MAX VALUE1Print“The largest value is”,MAXSTOPY NSTARTInputVALUE1,VALUE2MAX VALUE2isVALUE1>VALUE2
  25. 25. NESTED IFS One of the alternatives within an IF–THEN–ELSEstatement may involve further IF–THEN–ELSE statement
  26. 26. EXAMPLE 6 Write an algorithm that reads three numbers andprints the value of the largest number.
  27. 27. Step 1: Input N1, N2, N3Step 2: if (N1>N2) thenif (N1>N3) thenMAX N1 [N1>N2, N1>N3]elseMAX N3 [N3>N1>N2]endifelseif (N2>N3) thenMAX N2 [N2>N1, N2>N3]elseMAX N3 [N3>N2>N1]endifendifStep 3: Print “The largest number is”, MAX
  28. 28. EXAMPLE 7 Write and algorithm and draw a flowchart toa) read an employee name (NAME), overtime hoursworked (OVERTIME), hours absent (ABSENT)andb) determine the bonus payment (PAYMENT).
  29. 29. Step 1: Input NAME,OVERTIME,ABSENTStep 2: if (OVERTIME–(2/3)*ABSENT > 40) thenPAYMENT 50else if (OVERTIME–(2/3)*ABSENT > 30) thenPAYMENT 40else if (OVERTIME–(2/3)*ABSENT > 20) thenPAYMENT 30else if (OVERTIME–(2/3)*ABSENT > 10) thenPAYMENT 20elsePAYMENT 10endifStep 3: Print “Bonus for”, NAME “is $”, PAYMENT
  30. 30. Bonus ScheduleOVERTIME – (2/3)*ABSENT Bonus Paid>40 hours>30 but 40 hours>20 but 30 hours>10 but 20 hours10 hours$50$40$30$20$10
  31. 31. THANK YOU.

×