Example programming problem about braking distance
This section examines how to take a problem, decompose it and design an algorithm to solve it.
The following example has been taken from an AQA past paper. It reflects the type of question that may appear in an exam paper.
The problem
A programmer has been asked to design a program that calculates an estimate of the braking distance in metres for a new model of go-kart that is travelling between 10 and 50 kilometres per hour - km/h.
The program should keep asking the user to enter a speed for the go-kart until they enter a speed between 10 and 50 km/h. The braking distance in metres is calculated by dividing the speed by 5. The user should then be asked if the ground is wet and the braking distance should be multiplied by 1.5 if this is true. Finally, the program should output the calculated braking distance.
Decompose the problem
The first step is to break down - decompose - the overall problem into several smaller, more easily solved problems:
- Find out the speed of the go-kart.
- Validate the data entered is correct.
- Find out if the ground is wet.
- Work out which calculation to use for braking distance.
- Calculate the braking distance.
- Output the braking distance.
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 |
| Integer |
| String/Boolean* |
| Integer |
Variable |
|
---|---|
Data type | Integer |
Variable |
|
---|---|
Data type | String/Boolean* |
Variable |
|
---|---|
Data type | Integer |
*As user input is used here, a stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. is valid as they may answer yes/no.
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 |
| Real |
Constant |
|
---|---|
Data type | Real |