AQA GCSE Computer Science (8525), section 3.1.1 (Representing algorithms)
Inputs, processing and outputs
Every algorithm takes some input, does some processing, and gives some output.
- Input: data going into the algorithm (from the keyboard, a file, a sensor...).
- Processing: the calculations and decisions done with the data.
- Output: the result produced (shown on screen, saved, sent...).
Example
OUTPUT "Enter the radius"
radius ← USERINPUT (input)
area ← 3.14 * radius * radius (processing)
OUTPUT "The area is " + area (output)
In exam questions you may be asked to identify which lines are inputs, processing and outputs.
Determining the purpose of an algorithm
To find out what an algorithm does, you can:
- Read it carefully: visual inspection.
- Work through it by hand with some test data, using a trace table.
Trace tables
A trace table has a column for each variable (and one for any output). You work through the algorithm one line at a time, writing a new value each time a variable changes.
Example
a ← 1
b ← 10
WHILE a < b
a ← a + 3
b ← b - 1
ENDWHILE
OUTPUT a
| a | b | a < b? | Output |
|---|---|---|---|
| 1 | 10 | yes | |
| 4 | 9 | yes | |
| 7 | 8 | yes | |
| 10 | 7 | no | |
| 10 |
The loop stops when a < b is false, so the output is 10.
The purpose of this algorithm is not obvious from the numbers alone. Try different starting values to see the pattern.
Another example: finding a purpose
count ← 0
FOR i ← 1 TO 5
n ← USERINPUT
IF n MOD 2 = 0 THEN
count ← count + 1
ENDIF
ENDFOR
OUTPUT count
Trace with the inputs 4, 7, 10, 3, 8:
| i | n | n MOD 2 | count |
|---|---|---|---|
| 0 | |||
| 1 | 4 | 0 | 1 |
| 2 | 7 | 1 | 1 |
| 3 | 10 | 0 | 2 |
| 4 | 3 | 1 | 2 |
| 5 | 8 | 0 | 3 |
Output: 3. The algorithm counts how many even numbers were entered.
Python check
a, b = 1, 10
while a < b:
a = a + 3
b = b - 1
print(a) # 10
count = 0
for n in [4, 7, 10, 3, 8]:
if n % 2 == 0:
count += 1
print(count) # 3
Common mistake: missing out a row of the trace table, or changing more than one variable in a single row without showing it. Do one line at a time. Also, remember the final check of a loop condition (the row where it becomes false).
Exam tip: write the column headings first, using the variable names from the algorithm. Update only the cell that changes, and put anything output in its own column.
Check yourself
In the line `area ← 3.14 * radius * radius`, is this input, processing or output?
Processing.
What does a trace table help you to do?
Work out what an algorithm does, and find errors, by recording the values of variables as each line is carried out.
An algorithm sets total to 0 and adds each number entered to it, then outputs total. What is its purpose?
It adds up (finds the total of) the numbers that are entered.