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 breaking distance in metres is calculated by dividing the speed by 5. The user should then be asked if the ground is wet and the breaking 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:

  1. Find out the speed of the go-kart.
  2. Validate the data entered is correct.
  3. Find out if the ground is wet.
  4. Work out which calculation to use for braking distance.
  5. Calculate the braking distance.
  6. Output the braking distance.

Variables and constants

From the above, the solution will require the following variables. These values will change as the program is run.

VariableData type
speedInteger
wet_groundString/Boolean*
braking_distInteger

*As user input is used here, a string is valid as they may answer yes/no.

The following constants will be used. These values will not change when the program is run.

ConstantData type
wet_calcReal