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.
A programmer has been asked to design a program that enables a player to play the game rock paper scissors. The choices of rock, paper, and scissors are stored in an array [“paper”, “rock”, “scissors”]. The player enters the number 1 for paper, number 2 for rock, and number 3 for scissors. If both the user and the computer choose the same number, they draw. At the end of the round, the program outputs which choice beat the other, eg “rock beats scissors” or “draw”.
The first step is to break down - decompose - the overall problem into several smaller, more easily solved problems:
From the step above, the solution will require the following variables. These values will change as the program is run.
|Array of string|
*As user input is needed here, a string is valid as they may answer yes/no.
In this example, no constants are used as the only data that doesn’t change is stored in an array. Not every algorithm uses constants.