Problem solving Original Source http www ftsm ukm
Problem solving Original Source : http: //www. ftsm. ukm. my/zma/TK 1914/05 -Algorithms and Problem Solving. ppt 1
PROBLEM SOLVING � Programming � Problem is a process of problem solving techniques ◦ Analyze the problem ◦ Outline the problem requirements ◦ Design steps (algorithm) to solve the problem � Algorithm: ◦ Step-by-step problem-solving process ◦ Solution achieved in finite amount of time 2
PROBLEM SOLVING PROCESS � Step 1 - Analyze the problem � Step 2 - Implement the algorithm � Step 3 - Maintenance ◦ Outline the problem and its requirements ◦ Design steps (algorithm) to solve the problem ◦ Implement the algorithm in code ◦ Verify that the algorithm works ◦ Use and modify the program if the problem domain changes 3
EXAMPLE 1: RECTANGLE � 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 4
EXAMPLE 1 � Requirements: ◦ Input: length and width of the rectangle ◦ Output: perimeter and area of the rectangle ◦ Process: perimeter = ? ? ? , area =? ? ? 5
EXAMPLE 1 � 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 6
EXAMPLE 2: CALCULATE CAR PARK CHARGE A car park has the following charges: The 1 st hour costs RM 2. 00. The subsequent hours cost RM 1. 00 per hour. Write an algorithm to calculate the charges based on a vehicle’s entry and exit time. Input • Entry_time • Exit_time Process ? ? Output Charge 7
EXAMPLE 2: FLOWCHART Start Input Entry_time Input Exit_time Period Exit_time – Entry_time Charge 2 No Yes Charge 2 + (Period * 1) Period > 1? Output Charge End 8
EXAMPLE 2: FLOWCHART Start Input Entry_time Input Exit_time Period Exit_time – Entry_time Charge 2 cin >> entry_time >> exit_ti period = exit_time – entry_t Period > 1? Charge 2 + (Period * 1) No Yes Output Charge End if (period > 1) charge = 2 + ( period *1); else charge = 2; 9 cout <<charge;
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; } 10
EXAMPLE 3: PAYCHECK �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. 11
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 12
EXAMPLE 3 �Requirements: ◦ Input: base salary, number of years work, total sale ◦ Output: amount of paycheck (total salary) ◦ Process: ? ? ? 13
EXAMPLE 3 � Algorithm: ◦ Get base. Salary ◦ Get no. Of. Service. Years ◦ Calculate bonus using the following formula: if (no. Of. Service. Years <= 5) bonus = 10 * no. Of. Service. Years otherwise bonus = 20 * no. Of. Service. Years ◦ Get total. Sale 14
EXAMPLE 3 ◦ Calculate additional. Bonus as follows: if (total. Sale < 5000) additional. Bonus = 0 otherwise if (total. Sale>=5000 and total. Sale<10000) additional. Bonus = total. Sale x(0. 03) otherwise additional. Bonus = total. Sale x (0. 06) 15
EXAMPLE 3 ◦ Calculate pay. Check using the equation pay. Check = base. Salary + bonus + additional. Bonus 16
EXAMPLE 4: AVERAGE TEST SCORE � 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. 17
EXAMPLE 4 � Algorithm 1: to find test score ◦ Get the five test scores. ◦ Add the five test scores. Suppose sum stands for the sum of the test scores. ◦ Suppose average stands for the average test score. Then average = sum / 5; 18
EXAMPLE 4 � Algorithm 2: to determine the grade. if average > 90 grade = A otherwise if average >= 80 and < 90 grade = B otherwise if average >= 70 and < 80 grade = C otherwise if average >= 60 and < 70 grade = D otherwise grade = F 19
EXAMPLE 4 � Main algorithm: ◦ total. Average = 0; ◦ Repeat the following steps for each student in the class. �Get student’s name. �Use algorithm 1. �Use the algorithm 2. �Update total. Average by adding current student’s average test score. ◦ Determine the class average as follows: class. Average = total. Average / 10 20
- Slides: 20