05-Algorithms and Problem Solving
05-Algorithms and Problem Solving
1. Start
2. Prepare a Breakfast
3. End
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2 Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
2.3 Make a cup of coffee
3. End
FTSM :: TK1914 20112012 7
‘DIVIDE AND CONQUER’ STRATEGY
IN ALGORITHM
1. Start
2. Prepare a Breakfast
2.1. Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2. Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
2.3. Make a cup of coffee
2.3.1 Boil water
2.3.2 Add water with sugar and coffee
3. End
FTSM :: TK1914 20112012 8
CLASS ACTIVITY 5.1
Symbol Semantic
Start/End
Process
Input/Output
Test
Connector
Flow of activities
Input
Gregorian date Input data from user
Convert Gregorian
date to Islamic date Perform the date conversion
Display
Display the result
Islamic date
1. Start
2. Read quantity
3. Read price_per_kg
4. price quantity * price_per_kg
5. Print price
6. End
Start
• length, width and
Input
area are referred to
length, width as variables.
• A variable is like a box
in which a value can
area ← length X width
be stored
Output
area
End
FTSM :: TK1914 20112012 20
FLOWCHART: EXAMPLE 3
• Selection Start
Input
height
false true
height > 1.6?
Output Output
“You are short!” “You are tall!”
End
Output
“Thank you!”
Input
stop
false
stop = 1?
true
End
FTSM :: TK1914 20112012 22
PROBLEM SOLVING
• Problem:
Design an algorithm to find the perimeter and
area of a rectangle.
• Information:
The perimeter and area of the rectangle are given
by the following formulas:
perimeter = 2 * (length + width)
area = length * width
• Requirements:
– Input: length and width of the rectangle
– Output: perimeter and area of the rectangle
– Process: perimeter = ???, area =???
• Algorithm:
– Get length of the rectangle
– Get width of the rectangle
– Find the perimeter using the following
equation:
perimeter = 2 * (length + width)
– Find the area using the following equation:
area = length * width
– Display the result perimeter and area
No Yes
No Yes
if (period > 1)
charge = 2 + ( period *1);
else
charge = 2;
cout <<charge;
FTSM :: TK1914 20112012 31
EXAMPLE 2: C++ PROGRAM
void main() {
int entry_time, exit_time, period, charge;
cin >>entry_time >>exit_time;
period = exit_time – entry_time;
if (period > 1)
charge = 2 + (period * 1);
else
charge = 2;
cout <<charge;
}
• Problem:
Design an algorithm to calculate a paycheck of a
salesperson.
• Information:
– Every salesperson has a base salary.
– Salesperson receives $10 bonus at the end of
the month for each year worked if he or she
has been with the store for five or less years.
– The bonus is $20 for each year that he or she
has worked there if over 5 years.
FTSM :: TK1914 20112012 33
EXAMPLE 3
• Information (continue):
Additional bonuses are as follows:
– If total sales for the month are $5,000-$10,000,
he or she receives a 3% commission on the
sale
– If total sales for the month are at least $10,000,
he or she receives a 6% commission on the
sale
• Requirements:
– Input: base salary, number of years work,
total sale
– Output: amount of paycheck (total salary)
– Process: ???
• Algorithm:
– Get baseSalary
– Get noOfServiceYears
– Calculate bonus using the following formula:
if (noOfServiceYears <= 5)
bonus = 10 * noOfServiceYears
otherwise
bonus = 20 * noOfServiceYears
– Get totalSale
• Problem:
– 10 students in a class
– Each student has taken five tests and each test
is worth 100 points.
– Design an algorithm to calculate the grade for
each student as well as the class average.
• Design an algorithm to find the average test score.
• Design an algorithm to determine the grade.
– Data consists of students’ names and their test
scores.
FTSM :: TK1914 20112012 39
EXAMPLE 4
• Main algorithm:
– totalAverage = 0;
– Repeat the following steps for each student in the
class.
• Get student’s name.
• Use algorithm 1.
• Use the algorithm 2.
• Update totalAverage by adding current student’s
average test score.
– Determine the class average as follows:
classAverage = totalAverage / 10
FTSM :: TK1914 20112012 42
PROGRAM STYLE AND FORM
int main() {
…
// Convert elapsed_time to min:sec
min = elapsed_time / 60;
sec = elapsed_time % 60;
// Convert min:sec to hr:min:sec
hr = min / 60;
min = min % 60;
…
}