You are on page 1of 21

Chapter 3: Algorithm

Session Plan (Week 3):


To understand:
the concept of algorithm
algorithm representation using
•flowchart
•pseudo code
What is Algorithm?
Consider the following
Problem: Baking a Cake
1. Start
2. Preheat the oven at 180°C
3. Prepare a baking pan
4. Beat butter with sugar
5. Mix them with flour, eggs and essence vanilla
6. Pour the dough into baking pan
7. Put the pan into oven
8. End

TMK 3102-Chap 3: Algorithm 2


Algorithm (Definition)
 A specific and step by step set of instructions for carrying
out a procedure or solving a problem, usually with the
requirement that the procedure terminate at some point
 2 types of algorithm presentation will be discussed
• Flowchart
• Pseudo-code

TMK 3102-Chap 3: Algorithm 3


Flowchart

Flowchart represents
algorithm graphically. It
is intended for
communication and
documentation

TMK 3102-Chap 3: Algorithm 4


Pseudo code
 An outline of program, written in a form that
easily can be converted to real programming
language statements. It resembles the actual
program that will be implemented later.
However it can not be compiled or executed
 Pseudo code normally code the following
actions
• Initialization of variables
• Assignment of values to the variables
• arithmetic operations
• Relational operations

TMK 3102-Chap 3: Algorithm 5


Flowchart Basic
Symbol Semantic

Start/End
Process

Input/Output

Test Condition

Flow of activities

TMK 3102-Chap 3: Algorithm 6


Process of Problem Solving

Input PROCESS Output

TMK 3102-Chap 3: Algorithm 7


Example 1

Calculate and display the price of an apples, if the


quantity in kg and price per kg are given.

Input PROCESS Output

 Quantity Total Price


Calculate Price
(kg)
 Price per kg Quantity x Price per kg

TMK 3102-Chap 3: Algorithm 8


Flowchart of Example 1
start

get quantity

get price_per_kg

price = quantity x price_per_kg

Display price

end

TMK 3102-Chap 3: Algorithm 9


Pseudocode of Example 1
1. Start
2. Get quantity of apple
3. Get price_per_kg
4. Price = quantity x price_per_kg
5. Display Price
6. End

TMK 3102-Chap 3: Algorithm 10


Example 2
Determine a number given either it is even or odd
number. If the number is an even, display message
“Number is even” and if the number is odd display
message “Number is odd”

Input PROCESS Output

a number Display message


Determine the type

If number % 2 = 0 then message = “Number is even”


If number % 2 ≠ 0 then message = “Number is odd”

TMK 3102-Chap 3: Algorithm 11


Flowchart of Example 2
start

get number

Message = true false Message =


Number / 2 = 0
“Number is even” “Number is odd”

Display Message

end

TMK 3102-Chap 3: Algorithm 12


Pseudo code of Example 2
1. Start
2. Get a Number
3. Divide number by 2
3.1 If remainder = 0 , message = “Number is even”
3.2 If remainder ≠ 0, message = “Number is odd”
4. Display message
5. End

TMK 3102-Chap 3: Algorithm 13


General Approach to developing
an Algorithm
Asked 5 questions?
1. What is required?

2. What is available to solve the


problems?
3. What calculations are required?

4. What decision may need to be made?

5. What steps are repeated?

TMK 3102-Chap 3: Algorithm 14


Exercise 1
a. Recognized the input, the process and the output for
the problem of computing the area of circle
Input : radius
Process : area = π x r x r
Output : area

b. Write the algorithm using pseudo code.


1. Start
2. Get the value of radius
3. Calculate area where area = π x r x r
4. Display area
5. End

TMK 3102-Chap 3: Algorithm 15


Exercise 2
a. Recognized the input, the process and the
output for the problem of computing the
average of 5 numbers
Input : number1, number2, number3, number4, number5
Process :
Total of Number = number1 + number2 +
number3 + number4 + number5
Average = Total of Number / 5
Output : Display Average

TMK 3102-Chap 3: Algorithm 16


Exercise 2
b. Write the algorithm using pseudo code.
1. Start
2. Get the value of number1
3. Get the value of number2
4. Get the value of number3
5. Get the value of number4
6. Get the value of number5
7. Calculate the average
• Total number = number1 + number2 + number3 + number4
+ number5
• Average = Total Number / 5
8. Display Average
9. End

TMK 3102-Chap 3: Algorithm 17


Exercise 3
a. Recognized the input, the process and the
output for the problem of computing the total
amount of EPF deduction for a month
Input : Salary
% employee Deduction
% employer Deduction
Process : employee deduction = % employee Deduction x Salary
employer deduction = % employer Deduction x Salary
total Deduction = employee deduction + employer deduction
Output : total deduction

TMK 3102-Chap 3: Algorithm 18


Exercise 3
b. Write the algorithm using pseudo code.
1. Start
2. Get the salary
3. Get % of employee deduction
4. Get % of employer deduction
5. Calculate the EPF deduction
• employee deduction = % employee deduction x Salary
• employer deduction = % employer deduction x Salary
• EPF deduction = employee deduction + employer
deduction
• Display EPF deduction
• End
TMK 3102-Chap 3: Algorithm 19
Exercise 4
For all three (3) exercises above, draw the
flowchart

TMK 3102-Chap 3: Algorithm 20


Thank You

TMK 3102-Chap 3: Algorithm 21

You might also like