大象传媒

Decomposition and algorithm practice questions - AQAExample programming problem about computer games

Every programming problem needs decomposing so that it can be properly understood. From this, an algorithm can be designed and tested.

Part of Computer ScienceStudy skills

Example programming problem about computer games

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 enables a player to play the game rock paper scissors. The choices of rock, paper, and scissors are stored in an array [鈥減aper鈥, 鈥渞ock鈥, 鈥渟cissors鈥漖. 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 鈥渞ock beats scissors鈥 or 鈥渄raw鈥.

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 player鈥檚 choice.
  2. Assign a random choice for the computer.
  3. Work out if the outcome was a draw.
  4. If not, work out which choice won.
  5. Output the winning choice over the losing choice.

Variables and constants

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

VariableData type
options
Array of string
player_choice
Integer
comp_choice
Integer
Winner
String
Variable
options
Data typeArray of string
Variable
player_choice
Data typeInteger
Variable
comp_choice
Data typeInteger
Variable
Winner
Data typeString

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

In this example, no are used as the only data that doesn鈥檛 change is stored in an array. Not every algorithm uses constants.