 Download Download Presentation Problem solving

# Problem solving

Download Presentation ## Problem solving

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Problem solving Original Source : http://www.ftsm.ukm.my/zma/TK1914/05-Algorithms and Problem Solving.ppt

2. Problem Solving • Programming is a process of problem solving • 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

3. Problem Solving Process • Step 1 - Analyze the problem • Outline the problem and its requirements • Design steps (algorithm) to solve the problem • Step 2 - Implement the algorithm • Implement the algorithm in code • Verify that the algorithm works • Step 3 - Maintenance • Use and modify the program if the problem domain changes

4. 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

5. Example 1 • Requirements: • Input: length and width of the rectangle • Output: perimeter and area of the rectangle • Process: perimeter = ???, area =???

6. 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

7. Example 2: Calculate Car Park Charge A car park has the following charges: The 1st hour costs RM2.00. The subsequent hours cost RM1.00 per hour. Write an algorithm to calculate the charges based on a vehicle’s entry and exit time. Process Input Output • Entry_time • Exit_time ???? Charge

8. No Yes Example 2: Flowchart Start Input Entry_time Input Exit_time Period  Exit_time – Entry_time Period > 1? Charge 2 Charge  2 + (Period * 1) Output Charge End

9. No Yes Example 2: Flowchart cin >> entry_time >> exit_time; Start Input Entry_time Input Exit_time period = exit_time – entry_time; Period  Exit_time – Entry_time Period > 1? Charge 2 Charge  2 + (Period * 1) if (period > 1) charge = 2 + ( period *1); else charge = 2; cout <<charge; Output Charge End

10. 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; }

11. 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.

12. 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

13. Example 3 • Requirements: • Input: base salary, number of years work, total sale • Output: amount of paycheck (total salary) • Process: ???

14. Example 3 • Algorithm: • Get baseSalary • Get noOfServiceYears • Calculate bonus using the following formula: if (noOfServiceYears <= 5) bonus = 10 * noOfServiceYears otherwise bonus = 20 * noOfServiceYears • Get totalSale

15. Example 3 • Calculate additionalBonus as follows: if (totalSale < 5000) additionalBonus = 0 otherwise if (totalSale>=5000 and totalSale<10000) additionalBonus = totalSale x(0.03) otherwise additionalBonus = totalSale x (0.06)

16. Example 3 • Calculate payCheck using the equation payCheck = baseSalary + bonus + additionalBonus

17. 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.

18. 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;

19. 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

20. 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