algorithms & flowcharts
TRANSCRIPT
ALGORITHMS AND FLOWCHARTSALGORITHMS AND FLOWCHARTS
A typical programming task can be divided A typical programming task can be divided into two phases:into two phases:
Problem solving phaseProblem solving phase produce an ordered sequence of steps that produce an ordered sequence of steps that
describe solution of problemdescribe solution of problem this sequence of steps is called an this sequence of steps is called an algorithmalgorithm
Implementation phaseImplementation phase implement the program in some programming implement the program in some programming
languagelanguage
Steps in Problem SolvingSteps in Problem Solving
First produce a general algorithm (one can First produce a general algorithm (one can use use pseudocodepseudocode) )
Refine the algorithm successively to get Refine the algorithm successively to get step by step detailedstep by step detailed algorithm algorithm that is that is very close to a computer language.very close to a computer language.
PseudocodePseudocode is an artificial and informal is an artificial and informal language that helps programmers develop language that helps programmers develop algorithms. Pseudocode is very similar to algorithms. Pseudocode is very similar to everyday English.everyday English.
PseudocodePseudocode & Algorithm& Algorithm
Example 1:Example 1: Write an algorithm to Write an algorithm to determine a student’s final grade determine a student’s final grade and indicate whether it is passing or and indicate whether it is passing or failing. The final grade is calculated failing. The final grade is calculated as the average of four marks.as the average of four marks.
PseudocodePseudocode & Algorithm& Algorithm
PseudocodePseudocode:: Input a set of 4 marksInput a set of 4 marks Calculate their average by summing and Calculate their average by summing and
dividing by 4dividing by 4 if average is below 50if average is below 50
Print “FAIL”Print “FAIL”elseelse
Print “PASS”Print “PASS”
PseudocodePseudocode & Algorithm& Algorithm
Detailed Algorithm Detailed Algorithm Step 1: Step 1: Input M1,M2,M3,M4Input M1,M2,M3,M4
Step 2: Step 2: GRADE GRADE (M1+M2+M3+M4)/4 (M1+M2+M3+M4)/4
Step 3: Step 3: if (GRADE < 50) thenif (GRADE < 50) thenPrint “FAIL”Print “FAIL”
elseelsePrint “PASS”Print “PASS”
endifendif
The FlowchartThe Flowchart
(Dictionary) A schematic representation of (Dictionary) A schematic representation of a sequence of operations, as in a a sequence of operations, as in a manufacturing process or computer manufacturing process or computer program.program.
The FlowchartThe Flowchart
A FlowchartA Flowchart shows logic of an algorithmshows logic of an algorithm emphasizes individual steps and their emphasizes individual steps and their
interconnectionsinterconnections e.g. control flow from one action to the e.g. control flow from one action to the
nextnext
Flowchart Symbols Flowchart Symbols
BasicOval
Parallelogram
Rectangle
Diamond
Hybrid
Name Symbol Use in Flowchart
Denotes the beginning or end of the program
Denotes an input operation
Denotes an output operation
Denotes a decision (or branch) to be made. The program should continue along one of two 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 program
ExampleExample
PRINT“PASS”
Step 1: Input M1,M2,M3,M4Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then
Print “FAIL” else
Print “PASS” endif
START
InputM1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
ISGRADE<5
0
PRINT“FAIL”
STOP
YN
Example 2Example 2
Write an algorithm and draw a Write an algorithm and draw a flowchart to convert the length in flowchart to convert the length in feet to centimeter.feet to centimeter.
PseudocodePseudocode:: Input the length in feet (Lft)Input the length in feet (Lft) Calculate the length in cm (Lcm) by Calculate the length in cm (Lcm) by
multiplying LFT with 30multiplying LFT with 30 Print length in cm (LCM)Print length in cm (LCM)
Example 2Example 2
AlgorithmAlgorithm Step 1: Input LftStep 1: Input Lft Step 2: Step 2: Lcm Lcm Lft x 30 Lft x 30 Step 3: Step 3: Print LcmPrint Lcm
START
InputLft
Lcm Lft x 30
PrintLcm
STOP
Flowchart
Example 3 Example 3
Write an algorithm and draw a Write an algorithm and draw a flowchart that will read the two sides flowchart that will read the two sides of a rectangle and calculate its area.of a rectangle and calculate its area.
PseudocodePseudocode Input the width (W) and Length (L) of a Input the width (W) and Length (L) of a
rectanglerectangle Calculate the area (A) by multiplying L Calculate the area (A) by multiplying L
with Wwith W Print APrint A
Example 3Example 3
AlgorithmAlgorithm Step 1: Step 1: Input W,LInput W,L Step 2: Step 2: A A L x W L x W Step 3: Step 3: Print APrint A
START
InputW, L
A L x W
PrintA
STOP
Example 4 Example 4
Write an algorithm and draw a flowchart Write an algorithm and draw a flowchart that will calculate the roots of a that will calculate the roots of a quadratic equation quadratic equation
Hint: Hint: dd = sqrt ( ), and the roots = sqrt ( ), and the roots are: are: xx11 = (– = (–bb + + dd)/2)/2aa and and xx22 = (– = (–bb – – dd)/2)/2aa
2 0ax bx c 2 4b ac
Example 4Example 4
PseudocodePseudocode: : Input the coefficients (a, b, c) of the Input the coefficients (a, b, c) of the
quadratic equationquadratic equation Calculate Calculate dd Calculate Calculate xx11 Calculate Calculate x2x2 Print Print xx1 and 1 and x2x2
Example 4Example 4
AlgorithmAlgorithm: : Step 1: Step 1: Input a, b, cInput a, b, c Step 2: Step 2: dd sqrt ( sqrt (
)) Step 3: Step 3: xx1 1 (– (–bb + + dd) / (2 x ) / (2 x aa)) Step 4: Step 4: xx2 2 (– (–bb – – dd) / (2 x ) / (2 x aa)) Step 5: Step 5: Print Print xx1, 1, xx22
START
Inputa, b, c
d sqrt(b x b – 4 x a x c)
Printx1 ,x2
STOP
x1 (–b + d) / (2 x a)
X2 (–b – d) / (2 x a)
4b b a c
DECISION STRUCTURES DECISION STRUCTURES
The expression A>B is a logical expressionThe expression A>B is a logical expression it describes ait describes a condition condition we want to testwe want to test if A>B is true (if A is greater than B) if A>B is true (if A is greater than B)
we take the action on leftwe take the action on left print the value of A print the value of A if A>B is false (if A is not greater than if A>B is false (if A is not greater than
B) B) we take the action on rightwe take the action on right print the value of Bprint the value of B
DECISION STRUCTURESDECISION STRUCTURES
isA>B
Print B
Print A
Y N
IF–THEN–ELSE STRUCTUREIF–THEN–ELSE STRUCTURE
The structure is as followsThe structure is as follows
If condition then If condition then
true alternative true alternative
else else
false alternativefalse alternative
endifendif
IF–THEN–ELSE STRUCTUREIF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as The algorithm for the flowchart is as follows:follows:
If A>B then If A>B then print Aprint A
else else print Bprint B
endifendif
isA>B
Print B
Print A
Y N
Relational OperatorsRelational Operators
Relational OperatorsRelational Operators
OperatorOperator DescriptionDescription>> Greater thanGreater than
<< Less than Less than
== Equal toEqual to
Greater than or equal toGreater than or equal to
Less than or equal toLess than or equal to
Not equal toNot equal to
Example 5 Example 5 Write an algorithm that reads two values, Write an algorithm that reads two values,
determines the largest value and prints the determines the largest value and prints the largest value with an identifying message.largest value with an identifying message.
ALGORITHMALGORITHMStep 1: Step 1: InputInput VALUE1, VALUE2 VALUE1, VALUE2Step 2: Step 2: if (if (VALUE1 > VALUE2) VALUE1 > VALUE2) then then
MAX MAX VALUE1 VALUE1elseelse
MAX MAX VALUE2 VALUE2endifendif
Step 3: Step 3: Print “The largest value is”, MAXPrint “The largest value is”, MAX
Example 5 Example 5
MAX VALUE1
Print“The largest value is”,
MAX
STOP
Y N
START
InputVALUE1,VALUE2
MAX VALUE2
isVALUE1>VALUE2
NESTED IFS NESTED IFS
One of the alternatives within an IF–One of the alternatives within an IF–THEN–ELSE statementTHEN–ELSE statement may involve furthermay involve further IF–THEN–ELSE IF–THEN–ELSE
statement statement
Example 6Example 6
Write an algorithm that reads Write an algorithm that reads threethree numbers and prints the value of the numbers and prints the value of the largest number.largest number.
Example 6Example 6
Step 1: Step 1: InputInput N1, N2, N3N1, N2, N3Step 2: Step 2: if (if (N1>N2) N1>N2) thenthen
if (if (N1>N3) N1>N3) then then MAX MAX N1 N1 [N1>N2, N1>N3][N1>N2, N1>N3]
elseelse MAX MAX N3 N3 [N3>N1>N2][N3>N1>N2]
endifendifelse else
if (if (N2>N3) N2>N3) then then MAX MAX N2 N2 [N2>N1, N2>N3][N2>N1, N2>N3]
elseelse MAXMAX N3 N3 [N3>N2>N1][N3>N2>N1]
endifendifendifendif
Step 3: Step 3: Print “The largest number is”, MAXPrint “The largest number is”, MAX
Example 6Example 6
Flowchart: Draw the flowchart of Flowchart: Draw the flowchart of the above Algorithm.the above Algorithm.
Example 7Example 7
Write and algorithm and draw a Write and algorithm and draw a flowchart to flowchart to
a)a) read an employee name (NAME), read an employee name (NAME), overtime hours worked (OVERTIME), overtime hours worked (OVERTIME), hours absent (ABSENT) andhours absent (ABSENT) and
b)b) determine the bonus payment determine the bonus payment (PAYMENT). (PAYMENT).
Example 7Example 7
Bonus ScheduleBonus Schedule
OVERTIME – (2/3)*ABSENTOVERTIME – (2/3)*ABSENT Bonus PaidBonus Paid
>40 hours>40 hours>30 but >30 but 40 hours 40 hours>20 but >20 but 30 hours 30 hours>10 but >10 but 20 hours 20 hours 10 hours10 hours
$50$50$40$40$30$30$20$20$10$10
Step 1: Input NAME,OVERTIME,ABSENTStep 2: if (OVERTIME–(2/3)*ABSENT > 40) then PAYMENT 50 else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT 40 else if (OVERTIME–(2/3)*ABSENT > 20) then PAYMENT 30 else if (OVERTIME–(2/3)*ABSENT > 10) then PAYMENT 20 else PAYMENT 10 endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Example 7Example 7
Flowchart: Draw the flowchart of Flowchart: Draw the flowchart of the above algorithm?the above algorithm?
Flowchart with Flowchart with looploop
Start
Is light less than 50? Turn lamp on
Yes
No
Flowchart with Flowchart with looploop
Start
Is light less than 50? Turn lamp on
Yes
No
Turn lamp off
Flowchart with Flowchart with looploop
Start
Is light less than 50? Turn lamp on
Yes
No
Turn lamp off
Beep
Flowchart with Flowchart with looploop
Start
Is light less than 50? Turn lamp on
Yes
No
Turn lamp off
Beep
ScenariosScenarios
An automatic bell rings in a shop An automatic bell rings in a shop when somebody enterswhen somebody enters
Gates open when somebody Gates open when somebody approaches themapproaches them
The turnstile at the fun park The turnstile at the fun park registers people as they want to registers people as they want to enter and displays a green lightenter and displays a green light
7.6 T2a
Flowchart with Flowchart with looploop