Créer une présentation
Télécharger la présentation

Télécharger la présentation
## Problem solving

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Problem solving**Original Source : http://www.ftsm.ukm.my/zma/TK1914/05-Algorithms and Problem Solving.ppt**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**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**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**Example 1**• Requirements: • Input: length and width of the rectangle • Output: perimeter and area of the rectangle • Process: perimeter = ???, area =???**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**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**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**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**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; }**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.**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**Example 3**• Requirements: • Input: base salary, number of years work, total sale • Output: amount of paycheck (total salary) • Process: ???**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**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)**Example 3**• Calculate payCheck using the equation payCheck = baseSalary + bonus + additionalBonus**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.**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;**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**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