3 Chapter 2 (Part II) - Algorithmic Problem Solving
3 Chapter 2 (Part II) - Algorithmic Problem Solving
3 Chapter 2 (Part II) - Algorithmic Problem Solving
Algorithmic
Problem Solving
Learning Outcome:
INPUT :
Numbers of hours
worked
PROCESS:
Process: The total salary of an employee per day is calculated as RM7.50 per hour for the
first eight hours and the remaining hours is calculated as RM11.25 per hour.
Error Handling: The input Number of Hours Worked must be a positive real number. If it is a
negative number or other non-acceptable character, the program will force the
user to re-enter the information.
Example 1 : Pseudocode
1. Start
2. Read the numbers of Hours worked
2.1 If numbers of hours works <0 or numbers of hours works > 24
Print “Invalid input”
3. Calculate the Total Salary:-
3.1 If the numbers of Hours Worked <= 8
Total Salary = number of Hours worked * RM 7.50
3.2 Otherwise
Total Salary = (8 * RM7.50) +( (Number of Hours worked-8) *
RM 11.25)
4. Print Total Salary
5. End
Start
7
if the number of No
Hours Worked Print Error Message
< 0 OR > 24 “Invalid hours”
Yes
Check if the
number of
Yes Hours Worked No
<=8
Calculate Total Salary Calculate Total Salary
Total Pay = RM7.50 * Total Pay = (8 * RM7.50) +
Number of Hours worked ( (Number of Hours worked-8) *
RM 11.25)
End
8
Step 3 – Coding
Algorithm is translated into programming
language
9
Example 1 : Program
10
Example 1 : Output
11
Example 2 : Problem
Identification GOAL : To calculate and
INPUT : print the value of force
(p)
Time (t)
PROCESS:
OUTPUT:
If (time <= 3 sec)
then, p = 20 The value of force
Time MUST Else ( time > 3
be positive sec) then
p = 4 (time + 2)
12
13
Example 2: Pseudocode
1. Start
2. Get time (t)
If time(t) less than 0
Print ERROR MESSAGE
3. If time <= 3 seconds then
Print message : Force (p) = 20
Else
Calculate Force (p) = 4(time + 2)
Print message : The value of force
4. End
14
Example 2 : Flowchart
Set time and force as Get time (seconds) from user
Start
double
NO
YES NO
If Time <= 3
seconds
Print value of
End force (p)
15
Example 3: Pseudocode
1. Start
2. Get weight (kg)
While weight < 0
Print ERROR MESSAGE and ask user to RE-ENTER valid weight
3. Get height (m)
While height < 0
Print ERROR MESSAGE and ask user to RE-ENTER valid height
4. Calculate Body Mass Index (BMI)
BMI = kg / meter * meter
5. Print BMI
6. If BMI > 30 then
Print message : You are OVERWEIGHT
Else
Print message : You are NORMAL
7. End
Example 3 : Flowchart
Set weight, height
Get weight
Start and bmi as double
YES
Weight < 0? Print “Weight
cannot be
NEGATIVE.”
NO
Get height
Print “Height
Calculate Body Mass Index (BMI) NO YES
Height < 0 ? cannot be
NEGATIVE.”
BMI = kg / meter * meter
Print BMI
NO
If BMI >
YES 30
Print “YOU ARE OVERWEIGHT” Print “YOU ARE NORMAL”
End
20
21
Example 4 : Newtons
Scientists measure an object’s mass in kilograms(kg) and its
weight in Newtons (N).
Mass is the resistance of an object to a change in its motion,
while weight is the gravitational force of attraction between
the object and the Earth
If you know the amount of mass that an object has, you can
calculate its weight, in Newtons with the following formula:
Weight = mass x 9.8
Write a program that asks the user to enter an object’s mass,
and then calculates and displays its weight. If the object
weighs more than 1,000 Newtons, display a message
indicating that it is too heavy. If the object weighs less that 10
Newtons, display a message indicating that the object is too
light
24
INPUT :
Mass of an object
PROCESS:
Example 4 : Pseudocode
1. Start
2. Get a mass of an object
3. While mass of an object < 0
Print message : Mass cannot be NEGATIVE. Ask the user to
reenter the mass object.
4. Calculate weight (Newton)
Weight = mass * 9.8
5. Print Weight
6. If Weight < 10 then
Print message : Weight is to light
7. If Weight > 10000 then
Print message : Weight is to heavy
8. End
Example 4 : Flowchart
Set weight, mass and
Start acceleration_value=9.8 as Get a mass of
double an object
NO
Calculate Weight(Newton)
Print Weight
YES NO
If Weight
<10
End
26
27
INPUT :
Example 5 - Pseudocode
1. Start
2. Read the base price of the item
3. Calculate total tax
3.1 Calculate state portion of the sales tax using the formula:
State Sales Tax = base price * 0.04
3.2 Calculate city’s portion of the sales tax using the formula:
City Sales Tax = base price * 0.015
3.3 Calculate the luxury sales tax using the following formula:
If (base price item > 50000)
Luxury Tax = base price * 0.10
Otherwise
Luxury Tax = 0
3.4 Total tax = state Sales Tax + city Sales Tax + Luxury Tax
4. Calculate the final price of the item
price sold = base price + Total Tax
5. Print the total tax and the final price of the item
6. End
32
Example 5 - Flowchart
Start
A
Set stateTaxRate = 0.04, cityTaxRate = 0.015, luxuryTaxRate=0.10,
basePrice, stateTax, cityTax, luxuryTax, totalTax and finalPrice as double
Calculate the final price of the item
FinalPrice = base price + TotalTax
Get the base price of the item
No Yes
If Base End
price >
50000 ?
Calculate luxury Tax
luxury Tax = 0 Calculate luxury Tax
luxury Tax = base price * 0.10
A
33
Exercise
1. Create pseudocode and flowchart that inputs 2 integers
n1 and n2. Then, calculate and print the value of n12 and
n23