2 Representation of Algorithms (continued) Flowcharts
3 Pseudo code ExampleWrite an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.Pseudo codeInput a set of 4 marksCalculate their average by summing and dividing by 4if average is below 60Print “FAIL”elsePrint “PASS”
5 Definition of Flowchart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows.A flowchartshows logic solution,emphasizes individual steps and their interconnections,A flowchart must have a start and stop,A steps in a flowchart must connect. Can’t leave a step “hanging” with no connection.
6 Flow ChartsFlowcharts are a graphical means of representing an algorithm.AdvantagesFlowchart permit the structure of a program to be easily visualized - even if all the text were to be removed.The human brain is very good at picking out these patterns and keeping them "in the back of the mind" as a reference frame for viewing the code as it develops.
7 Basic Flowchart Shapes Terminal symbol - indicates the beginning andend points of an algorithm.Process symbol - shows an instruction other thaninput, output or selection.Input-output symbol - shows an input or an outputoperation.Disk storage I/O symbol - indicates input from or output to disk storage.
8 Basic Flowchart Shapes Selection symbol - shows a selection processfor two-way selection.Off-page connector - provides continuation of a logical path on another page.On-page connector - provides continuationof logical path at another point in the samepage.Flow lines - indicate the logical sequence ofexecution steps in the algorithm.
9 Flowchart – sequence control structure Statement 2Statement 1Statement 3:
10 Flowchart – selection control structure Conditionelse-statement(s)then-YesNo
11 Flowchart – repetition control structure ConditionLoopStatement(s)yesno
12 Age = current year – birth date Flowchart – example 1BeginRead birth dateCalculateAge = current year – birth dateDisplayageEnd
13 Flowchart – example 2 Read age Age > 55? NO YES print “Pencen” BeginRead ageEndAge > 55?NOYESprint “Pencen”print “Kerja lagi”
14 Flowchart – example 3 sum = 0 current_number = 1 NO BeginEndcurrent_number <= 10?NOYESsum = 0current_number = 1sum = sum + current_numbercurrent_number = current_number + 1print sum
15 AssignmentWrite an algorithm and draw a flowchart for a computer program that wouldread an employee name (NAME), overtime hours worked (OVERTIME), hours absent (ABSENT) anddetermine the bonus payment (PAYMENT).
16 Bonus Schedule OVERTIME – (2/3)*ABSENT Bonus Paid >40 hours $50 >30 but ≤ 40 hours $40>20 but ≤ 30 hours $30>10 but ≤ 20 hours $20≤ 10 hours $10