AQA GCSE Computer Science June 2023 Paper 1, Question 12.4: fixing a subroutine and a FOR loop
AQA 8525 June 2023 Paper 1, Question 12.4: rewrite two lines of pseudo-code so that a subroutine displays a list in reverse order. The parameter error and the loop error explained, with the fixed algorithm to run.
Question 12.4 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) gives you eight lines of pseudo-code with logic errors in them and asks you to rewrite two of the lines. It is worth 3 marks: one for each thing you put right.
We do not copy the exam paper here. Open it beside this page: AQA June 2023 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The algorithm should display eight currency names in reverse alphabetical order, starting with yen.
- A subroutine,
diffCurrencies, makes a list of the eight names in alphabetical order (baht first, yen last) and returns the one at positionx. - The main program has a FOR loop that calls the subroutine with
iand outputs what comes back.
You are told to rewrite line 1, the subroutine's heading, and line 6, the FOR loop.
Find the errors
Line 1: the parameter has the wrong name. The heading says the subroutine takes a parameter called currencies. But inside, currencies is the list, and the value it uses to choose a name is x. Nothing ever gives x a value. The number passed in should arrive as x:
SUBROUTINE diffCurrencies(x)
Line 6: the loop starts in the wrong place. Eight items have the index numbers 0 to 7. The loop on the paper starts at 8, which is past the end of the list. To start with yen it must start at 7.
Line 6 again: the loop counts the wrong way. It has STEP 1, which counts upwards. Starting high and going down to 0 needs STEP -1.
FOR i ← 7 TO 0 STEP -1
The answers
| Line | Rewritten |
|---|---|
| 1 | SUBROUTINE diffCurrencies(x) |
| 6 | FOR i ← 7 TO 0 STEP -1 |
One mark for the parameter, one for the 7 and one for the step. Small slips in copying the rest of the line, such as spelling or a missing bracket, are ignored.
Where the marks are lost
- Changing the body and not the heading. You could make the subroutine work by changing line 3 to use
currencies, but then the list and the parameter would share a name, and the question asks for line 1. - Fixing only the step.
FOR i ← 8 TO 0 STEP -1still asks for position 8, which does not exist. Count the items, then subtract one. - Forgetting that 0 is included.
7 TO 0visits eight values: 7, 6, 5, 4, 3, 2, 1, 0. That is right for eight names.
Run it
The corrected algorithm in Python. range(7, -1, -1) is how Python says "from 7 down to 0": the middle number is where it stops before, so it has to be -1.
The program
from bugbot import *
connect()
# the paper's version started at 8. Try it
START = 7
def diff_currencies(x):
currencies = ["baht", "dollar", "euro", "koruna", "lira", "rand", "rupee", "yen"]
return currencies[x]
for i in range(START, -1, -1):
print(diff_currencies(i))
With START = 8 Python stops with IndexError: list index out of range. That is the error in words: the index is outside the list.
Questions
What is the answer to AQA GCSE Computer Science 2023 Paper 1 Question 12.4?
Line 1 becomes SUBROUTINE diffCurrencies(x). Line 6 becomes FOR i ← 7 TO 0 STEP -1. The parameter must be called x because that is the name the subroutine uses, and the loop must start at the last index, 7, and count down.
What is a parameter?
A parameter is a named value that a subroutine receives when it is called. The name in the subroutine's heading must be the name the subroutine's code uses.
How do you count down with a FOR loop?
In AQA pseudo-code add STEP -1, as in FOR i ← 7 TO 0 STEP -1. In Python use a range with three numbers: range(7, -1, -1) counts from 7 down to 0.
More from this paper
- Question 2: Read a nested WHILE loop that uses integer division 3 marks
- Question 4: Trace a Python function with three sets of inputs, then make it robust 4 marks
- Question 5: Trace a flowchart with a loop 3 marks
- Question 6: Fill the gaps in a login routine 4 marks
- Question 7: A ticket price with a group discount 6 marks
Every AQA 8525 question we have worked
Learn it step by step
- F4.2 Parameters and return values Functions and structured code
- F2.5 Count-controlled loops: for Decisions and loops
This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.