Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Chapter 2 (part II):

Algorithmic
Problem Solving

Dr. Mohd Suffian Sulaiman


Faculty of Computer & Mathematical Sciences
Universiti Teknologi MARA
2

Learning Outcome:

 Practice makes perfect


3

Example 1 : Parking Garage


An employee of a parking garage is paid by the
number of hours that he or she work per day. The
employee will be paid for RM7.50 for the first 8 hours
and RM11.25 for the remaining hours.

Write a program that will calculate and print the


salary of an employee per day. A user will enter
the numbers of hours that employee has worked for
the day and the system will then print out the total
salary of the employee.
4

Example 1: Parking Garage


GOAL : To calculate and print the
total salary of an employee at
parking garage

INPUT :

Numbers of hours
worked
PROCESS:

The total salary of an


employee per day is calculated OUTPUT:
Numbers of hours worked.
as RM7.50 per hour for the
MUST be a Total salary of an employee at
first eight hours and the
positive number parking garage
remaining hours is calculated
MUST not exceed 24 as RM11.25 per hour.
hours

Salary of a parking garage


employee
MUST be a
positive number
5

Example 1 : Parking Garage


Program Goal: To calculate the total salary of an employee at a parking garage.

Inputs: Number of Hours Worked........................ a positive number

Outputs: Total Salary .................................... a positive number

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.

Testing Plan: INPUT OUTPUT NOTES


8 8*7.50 = 60 Testing positive input

3 3*7.50 = 22.5 Testing positive input

12 8*7.50 + 4*11.25 = 105 Testing overtime input

–6 Error message/ask user to Handling error


reenter value
6

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

Example 1 : Set hoursWorked and


totalSalary as double

Flowchart Read the number of


Hours worked

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)

Print Total Salary

End
8

Step 3 – Coding
 Algorithm is translated into programming
language
9

Example 1 : Program
10

Example 1 : Output
11

Example 2 : Value of Force (p)


The problem statement is to calculate
and print the value of force (p). The value
of force (p) will be depending on the
input time (t). The value force will be
equal to 20 if the input time (t) is less or
equal than 3 seconds; otherwise the value
of force (p) will be calculated based on
the formula p= 4 (t+2).
12

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

Print “Time cannot


If Time YES be NEGATIVE.
<0

NO
YES NO
If Time <= 3
seconds

Print Force (p) Calculate Force (p) = 4


= 20 (t + 2)

Print value of
End force (p)
15

Example 2 : The program


16

Example 2 : The output


17

Example 3: Body Mass Index


Calculate and display the output of Body Mass
Index (bmi) where weight in kg (kg) and height in
meter (m) will be determined by the user.

Print BMI and display an indicator as ‘overweight’ if


BMI is more than 30. Other than that, print ‘normal’.
[ Formula: BMI =weight in kg/ height in meters2 ]
18

Example 3: Problem Identification


GOAL : To calculate and display
Body Mass Index (BMI) and
indicator
INPUT :
Weight (kg)
Height (m)
PROCESS:
OUTPUT:
BMI = kg / meter2
If (BMI > 30 ) The value of BMI
Weight and Height then, display and indicator
‘OVERWEIGHT’ whether
MUST be in +ve Else then, display ‘OVERWEIGHT’ or
number ‘NORMAL’ ‘NORMAL’
19

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 3: The program


22

Example 3: The output


23

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

Example 4 : Problem Identification


GOAL : To calculate and print
the weight (Newtons) of an
object.

INPUT :

Mass of an object

PROCESS:

Weight (Newton) = mass * OUTPUT:


9.8
Weigh of an object
Mass of an object
And the indicator of
MUST be If (weight > 1000) then
Weight is too heavy whether the object is too
positive –
If (weight <10) then heavy or too light
physicists
always define Weight is too light
mass to be
positive
25

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

Mass of YES Print “Mass cannot be


an object NEGATIVE. Please Re-enter”
< 0?

NO
Calculate Weight(Newton)

Weight = mass * 9.8

Print Weight

YES NO
If Weight
<10

Print “Weight is to NO YES


light” If
Weight
>1000
Print “Weight is to
heavy”

End
26
27

Example 4 : The Program


28

Example 4 : The Output


29

Example 5 : Price Sold


A price of an item sold in a particular state is calculated by
adding up the base price of the item with the total sales tax.
Write a program to calculate the price of the item sold in a
particular state. The base price will be the input to the system.

Total sales tax is calculated as follows:


The state’s portion of the tax is 4% of the base price, and the
city’s portion of the tax is 1.5% of base price. If the item is a
luxury item, such as car which more than RM50,000, then
there will be additional tax of 10% of base price.
30

Example 5 : Problem Identification


GOAL : To calculate the final
price of item sold

INPUT :

Base price of an item PROCESS:

Final price of an item sold


is calculated by;
OUTPUT:
Base price of an item Final price = base item +
Total Tax Final price of an item sold
•MUST be positive Total tax
Total Tax includes state’s
tax (4% of base price),
city’s tax (1.5% of base
price) and luxury tax (10%
of base price) if the base
price > 50000.
31

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

Calculate state portion of the sales tax using the formula:


stateSalesTax = base price * 0.04 Display final price of the
Calculate city’s portion of the sales tax using the formula: item & the total tax.
citySalesTax = base price * 0.015

No Yes
If Base End
price >
50000 ?
Calculate luxury Tax
luxury Tax = 0 Calculate luxury Tax
luxury Tax = base price * 0.10

Calculate Total tax


Total tax = stateSalesTax + citySalesTax + luxuryTax

A
33

Example 5 : The Program


34

Exercise
1. Create pseudocode and flowchart that inputs 2 integers
n1 and n2. Then, calculate and print the value of n12 and
n23

2. Create pseudocode and flowchart to compute and print


the net pay of an employee; given the pay rate and
number of hours work as the input. Assume that 9%
deduction amount of the gross pay.

You might also like