Lecture 03 Algorithms-And-Flowcharts
Lecture 03 Algorithms-And-Flowcharts
Lecture 03 Algorithms-And-Flowcharts
FLOWCHARTS
ICT 012
3
Example 1. Add three numbers
4
Solution:
1. Underline the nouns and adjectives used in
the specification → establish the input,
output component and any object that are
required.
5
• Defining diagram
Number1 total
Number2
Number3
6
2. Underline the verbs and adverbs used in the specification →
establish the action required.
7
• Defining diagram
8
3. Writing down the processing steps in an algorithm,
9
Example 2. Find average temperature
10
Step 1
11
• Defining diagram
12
Step 2
13
• Defining diagram
14
Steps in Problem Solving
Pseudocode:
• Input a set of 4 marks
• Calculate their average by summing and dividing by
4
• if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
• 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(1)
A Flowchart
•Shows logic of an algorithm
•Emphasizes individual steps and their
interconnections
•e.g. control flow from one action to the
next
Flowchart Symbols
Name Symbol Use in Flowchart
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Algorithm
Flowchart
• Step 1: Input Lft START
• Step 2: Lcm Lft x 30
• Step 3: Print Lcm Input
Lft
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Solution
Pseudocode
• Input the width (W) and Length (L) of a rectangle
• Calculate the area (A) by multiplying L with W
• Print A
Example 3
Algorithm Flowchart
• Step 1: Input W,L
• Step 2: AL x W START
• Step 3: Print A
Input
W, L
ALxW
Print
A
STOP
Example 4 (1)
Pseudocode:
• Input the coefficients (a, b, c) of the quadratic equation
• Calculate d
• Calculate x1
• Calculate x2
• Print x1 and x2
Example 4 (3)
START
• Algorithm:
• Step 1: Input a, b, c Input
a, b, c
• Step 2: d sqrt ( b b − 4 ) a c
• Step 3: x1 (–b + d) / (2 x a) d sqrt(b x b – 4 x a x c)
• 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
• 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 Yes
is
No
than B) we take the action on A>B
left
• print the value of A Print A Print B
Y N
is
A>B
Print A Print B
IF–THEN–ELSE STRUCTURE
Print Print
A B
FLOWCHART: Area of a rectangle
Start
• length, width and area
Input are referred to as
length, width variables.
• A variable is like a box
area ← length X width in which a value can be
stored
Output
area
End
34
FLOWCHART: Selection/ Decision
• Selection Start
Input
height
false true
height > 1.6?
Output Output
“You are short!” “You are tall!”
End
35
FLOWCHART: Repetition/loop
• Repetition (looping)
Start
Output
“Thank you!”
Input
stop
false
stop = 1?
true
End 36
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to
More Example (1)
ALGORITHM:
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
More Example (2)
START
Input
VALUE1,VALUE2
Yes is
No
VALUE1>VALUE2
Print
“The largest value is”,
MAX
STOP
NESTED IF STATEMENT
• Write an algorithm that reads three numbers and prints the value of the
largest number.
Example 1 (2)
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid