Decomposing the problem - example one
This section examines how to take a problem, decompose it and design an algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. to solve it.
The following example has been written by 大象传媒 Bitesize consultants as a suggestion of the type of problem that may appear in an exam paper.
The problem
An employee's weekly pay depends on the rate of pay and the number of hours worked per week. Employees work a minimum of one hour per week, and a maximum of 60. An employee who works more than 40 hours is paid 1.5 times the normal pay rate for all hours worked over 40. Normal pay is 拢10 per hour. A program is required to calculate and output the weekly pay for any employee.
Decompose the problem
The first step is to break down (decompose) the overall problem into several smaller, more easily solved problems:
- Find out how many hours are worked
- Find out how many of those hours are to be paid at the normal rate
- Find out how many, if any, of those hours are to be paid at the overtime rate
- Work out the pay for the normal hours
- Work out the pay for the overtime hours
- Total the pay for normal and overtime hours
- Output the result
Variables and constants
From the above, the solution will require the following variableA quantity that can change or that may take on different values. Variable also refers to a letter or symbol representing such a quantity.. These values will change as the program is run:
Variable | Data type |
hours_worked | Float |
overtime_hours | Float |
normal_pay | Float |
overtime_pay | Float |
total_pay | Float |
Variable | hours_worked |
---|---|
Data type | Float |
Variable | overtime_hours |
---|---|
Data type | Float |
Variable | normal_pay |
---|---|
Data type | Float |
Variable | overtime_pay |
---|---|
Data type | Float |
Variable | total_pay |
---|---|
Data type | Float |
The following constantA value in computer programming that does not change when the program is running. will be used. These values will not change when the program is run:
Constant | Data type |
HOURLY_RATE | Float |
OVERTIME_RATE | Float |
MAX_HOURS | Integer |
MIN_HOURS | Integer |
NORMAL_HOURS | Integer |
Constant | HOURLY_RATE |
---|---|
Data type | Float |
Constant | OVERTIME_RATE |
---|---|
Data type | Float |
Constant | MAX_HOURS |
---|---|
Data type | Integer |
Constant | MIN_HOURS |
---|---|
Data type | Integer |
Constant | NORMAL_HOURS |
---|---|
Data type | Integer |