Week 5 - Problem Solving and Design
Week 5 - Problem Solving and Design
WHAT IS AN ALGORITHM?
• An ALGORITHM sets out the steps to complete a
given task. It is a set of ordered steps for solving a
problem.
• ALGORITHM is usually shown as a flowchart or
pseudocode.
2
ALGORITHM IN REAL LIFE
• Consider the following …
Problem: baking a cake
How to solve:
1. Start
2. Preheat the oven at 180oc
3. Prepare a baking pan
4. Beat butter with sugar
5. Mix them with flour, eggs and vanilla essence
6. Pour the dough into the baking pan
7. Put the pan into the oven
8. End
3
Another example
1. Start
2. Fill the bath with water
3. Get into bath
4. Wash
5. Get out of bath
6. Empty the bath water
7. Get dressed
9. End
4
FLOWCHARTS
• An algorithm can be represented graphically using a
flowchart.
• A Flowchart shows diagrammatically the steps in
algorithm that are required for a task (sub-system)
and the order that they are to be performed.
• Flowcharts are an effective way to communicate the
algorithm that shows how a system or sub-system
works.
5
FLOWCHART NOTATIONS
Symbol Semantic
Start/End
Process
Input/Output
Test
Connector
Flow of activities
6
Start/End
7
Process
Process symbols are used to show when values are
assigned to an item variable like an assignment in
pseudocode.
Note: Assignment
statement is the one
where some value is being
assigned (given) to a
variable.
8
Input / Output
9
Decision
10
Flow Lines
11
Recap:
12
FLOWCHART: EXAMPLE 1
Start Algorithm starts here
14
EXAMPLE 3
Start
• length, width and area
Input are referred to as
length, width variables.
• A variable is like a box in
area ← length X width which a value can be
stored
Output
area
End
15
Flowchart for checking-for-the-alarm-
time sub-system
Flowchart for checking
the alarm time sub-
system.
16
EXAMPLE 4
• Selection Case Start
Input
height
false true
height > 1.6?
Output Output
“You are short!” “You are tall!”
End
17
EXAMPLE 4
• Selection Case Start
Input
N1, N2
false true
N1 > N2?
Output Output
“N2 is largest” “N1 is largest”
End
18
ALGORITHM
• START
• TURN ON THE TAPS
• REPEAT
• SEE HOW MUCH WATER IS IN BATH
• UNTIL BATH IS FULL
• TURN OFF THE TAPS
• END
20
Example:6
21
PSEUDOCODE
• PSEUDOCODE is a simple method of showing an
algorithm, using English-like words and
mathematical operators that are set out to look like
a program.
Example1:
START
Input Num1,Num2
SUM=Num1+num2
Print Sum
END
22
Example 2:
If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"
Algorithm:
End If
Start
Input height
If student’s height is greater than 5.5
Print “tall"
else
Print “short"
End If
Output Height
End
23
Example 7
24
Activity
• Have a look at the flowchart and pseudocode
below. What is the purpose of the algorithm that
they both represent?
25
26
Algorithm
• START
• INPUT NUM1, NUM2
• IF NUM1>NUM2 THEN
• PRINT NUM1 IS LARGEST
• ELSE
• PRINT NUM2 IS LARGEST
• END IF
• eND
29