COMPUTER SCIENCECOVERED BASICS ABOUTALGORITHMS AND FLOWCHARTS
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
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.
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.
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”
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.
THE FLOWCHARTA Flowchart shows logic of an algorithm emphasizes individual steps and their interconnections e.g. control flow from one action to the next
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
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
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)
Algorithm Step 1: Input Lft Step 2: Lcm Lft x 30 Step 3: Print LcmSTARTInputLftLcm Lft x 30PrintLcmSTOPFlowchart
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
Algorithm Step 1: Input W,L Step 2: A L x W Step 3: Print ASTARTInputW, LA L x WPrintASTOP
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
Pseudo code: Input the coefficients (a, b, c) of the quadraticequation Calculate d Calculate x1 Calculate x2 Print x1 and x2
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)
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
IF–THEN–ELSE STRUCTURE The structure is as follows:If condition thentrue alternativeelsefalse alternativeendif
IF–THEN–ELSE STRUCTURE The algorithm for the flowchart is as follows:If A>B thenprint Aelseprint BendifisA>BPrintBPrintAY N
RELATIONAL OPERATORSRelational OperatorsOperator Description> Greater than< Less than= Equal toGreater than or equal toLess than or equal toNot equal to
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
MAX VALUE1Print“The largest value is”,MAXSTOPY NSTARTInputVALUE1,VALUE2MAX VALUE2isVALUE1>VALUE2
NESTED IFS One of the alternatives within an IF–THEN–ELSEstatement may involve further IF–THEN–ELSE statement
EXAMPLE 6 Write an algorithm that reads three numbers andprints the value of the largest number.
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
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).
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
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