Lecture 7 - Algorithms and Flow Charts
Lecture 7 - Algorithms and Flow Charts
AND
FLOWCHARTS
Fall 2022
ALGORITHMS AND FLOWCHARTS
Problem Solving
Produce an ordered sequence of steps that describe the
solution
This sequence of steps is called an algorithm
Implementation
Implement the program in some programming language such as
Java, Python, C …
Program Development Life Cycle
Steps in Problem Solving
Pseudocode
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
A Flowchart
Shows logic of an algorithm
Emphasizes individual steps and their interconnections
START
Input
M1,M2,M3,M4
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
GRADE(M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print “FAIL”
N IS Y else
GRADE<5 Print “PASS”
0 endif
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Pseudocode
Algorithm Flowchart
Step 1: Input Lft START
Print
Lcm
STOP
Example 3
Pseudocode
Input the width (W) and Length (L) of a rectangle
Print A
Example 3
Algorithm START
Step 3: Print A
ALxW
Print
A
STOP
Example 4
Calculate x1
Calculate x2
Print x1 and x2
Example 4
START
Algorithm:
Input
Step 1: Input a, b, c a, b, c
Step 2: d sqrt ( b b 4 a c )
d sqrt(b x b – 4 x a x c)
Step 3: x1 (–b + d) / (2 x a)
Step 4: x2 (–b – d) / (2 x a) x1 (–b + d) / (2 x a)
Step 5: Print x1, x2
X2 (–b – d) / (2 x a)
Print
x1 ,x2
STOP
DECISION STRUCTURES
print B
Print Print
endif A B
IF–THEN–ELSE STRUCTURE
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to
Example 5
ALGORITHM
Input
VALUE1,VALUE2
Y is N
VALUE1>VALUE2
Print
“The largest value is”, MAX
STOP
NESTED IFS
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid