Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Original Source : and Problem and Problem Solving.ppt.

Similar presentations


Presentation on theme: "1 Original Source : and Problem and Problem Solving.ppt."— Presentation transcript:

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

2  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 2

3  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 3

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

5  Requirements: ◦ Input: length and width of the rectangle ◦ Output: perimeter and area of the rectangle ◦ Process: perimeter = ???, area =??? 5

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

7 7 A car park has the following charges: The 1 st 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. Entry_time Exit_time Exit_time Charge???? Input Process Output

8 8 YesNo

9 cin >> entry_time >> exit_time; period = exit_time – entry_time; if (period > 1) charge = 2 + ( period *1); else charge = 2; cout <<charge; 9 YesNo

10 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

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

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

13  Requirements: ◦ Input: base salary, number of years work, total sale ◦ Output: amount of paycheck (total salary) ◦ Process: ??? 13

14  Algorithm: ◦ Get baseSalary ◦ Get noOfServiceYears ◦ Calculate bonus using the following formula: if (noOfServiceYears <= 5) bonus = 10 * noOfServiceYears otherwise bonus = 20 * noOfServiceYears ◦ Get totalSale 14

15 ◦ 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) 15

16 ◦ Calculate payCheck using the equation payCheck = baseSalary + bonus + additionalBonus 16

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

18  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

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

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


Download ppt "1 Original Source : and Problem and Problem Solving.ppt."

Similar presentations


Ads by Google