Algorithms and Flowcharts Prepared by Dr Haitham Tayyar
Algorithms and Flowcharts Prepared by: Dr. Haitham Tayyar Cesar Esperanza 1
Algorithms and Flowcharts • An algorithm is a step-by-step procedure to solve a given problem • A flowchart is a graphical representation of an algorithm showing the steps involved. 2
Basic Flowchart Symbols TERMINATOR (START / STOP) DECISION DOCUMENTATION (PRINT-OUT) ON-PAGE CONNECTOR PREPARATION INPUT / OUTPUT OFF-PAGE CONNECTOR PROCESS PREDEFINED PROCESS or FUNCTION DIRECTION (FLOW) 3
Three Basic Flow Charting Constructs I. SEQUENCE II. SELECTION TRUE FALSE 4
III. REPETITION A FALSE TRUE B In some cases, one of the two blocks, A and B, will be empty. 5
Flowchart Examples 6
EXERCISES: SEQUENCE Draw a flowchart that will ask the user to input two numbers and will print the sum and product of these two numbers: SAMPLE DISPLAY: Input two numbers and then press <ENTER> key: 2. 5 6 Sum=8. 5 Product=15 7
START A = 0 B = 0 Sum = 0 Prod = 0 Input A, B Sum = A + B Prod = A * B Print Sum, Prod STOP 8
EXERCISES: SEQUENCE Draw a flowchart of an algorithm that will ask the user to input a temperature in Fahrenheit which is then converted to Celsius and printed: SAMPLE DISPLAY: Enter a temperature in Fahrenheit to be converted: 32 Temperature in C = 0 9
START F = 0 C = 0 Input F C = ( F – 32 ) * ( 5/9 ) Print C STOP 10
EXERCISES: IF-ELSE Draw a flowchart that will ask the user to input the name and grade of a student. The algorithm will then print the name and grade of the student plus whether they passed or failed: grade < 60 grade >= 60 “Fail” “Pass” SAMPLE DISPLAY: Input name and score and press <ENTER> key: John 66 Pass 11
START Name = “ “ Grade = 0 Rem = “ “ Input Name, Grade FALSE Grade >= 60 Rem = “Fail” TRUE Rem = “Pass” Print Name, Grade, Rem STOP 12
EXERCISES: IF-ELSE Draw a flowchart that will ask the user to input the name and score of an employee. The algorithm will then print the name and score of the employee as well as a remark about their performance as follows: [4. 5 to 5. 0] [4. 0 to 4. 5) [3. 5 to 4. 0) [3. 0 to 3. 5) Less than 3 “Outstanding” “Very Satisfactory” “Needs Improvement” “Poor” SAMPLE DISPLAY: Input name and score and press <ENTER> key: James 3. 2 Needs Improvement 13
START Name = “ “ Score = 0 Rem = “ “ Input Name, Score TRUE Rem = “Poor” Score < 3 TRUE Rem = “Needs Improvement” FALSE Score < 3. 5 TRUE Score < 4 Rem = “Satisfactory” TRUE Rem = “Very Satisfactory” FALSE Score < 4. 5 FALSE Rem = “Outstanding” Print Name, Score, Rem STOP 14
EXERCISES: LOOPS Draw a flowchart that will print the numbers from 1 to 10. SAMPLE DISPLAY: 1 2 3 4 5 6 7 8 9 10 15
Solution 1 Solution 2 START X = 1 X = 0 X <= 10 TRUE FALSE STOP X < 10 FALSE STOP TRUE Print X X = X + 1 Print X 16
EXERCISES: LOOPS 17
Solution 1 Solution 3 Solution 2 START Pi = 3. 1416 R = 1 A = 0 Pi = 3. 1416 R = 0 A = Pi * R R <= 5 FALSE R = R + 1 TRUE Print R, A A = Pi * R R <= 5 FALSE TRUE R = R + 1 Print R, A R <= 5 R = R + 1 A = Pi * R Print R, A FALSE STOP 18
No Exponent in C • Remember that in C language there is no symbol for calculating the power or exponent. • If you need to calculate power, use a loop to multiply a number as many times as necessary. 19
Pseudocodes If student's grade is greater than or equal to 60 Print "passed" else Print "failed“ Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Set the class average to the total divided by ten Print the class average. 20
- Slides: 20