Decomposing the problem – example one
This section examines how to take a problem, decompose it and design an algorithm 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 1 hour per week, and a maximum of 60. An employee who works more than 40 hours is paid 11⁄2 times the normal pay rate for all hours worked over 40. Normal pay is £10.00 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 variables. 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 constants 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 |