AQA GCSE Computer Science June 2022 Paper 1, Question 14.1: filling a 2D array with nested loops
AQA 8525 June 2022 Paper 1, Question 14.1: complete a Python program that fills a 3 by 3 bingo ticket using a while loop inside a while loop. Each gap explained, where the marks go, and the finished program to run.
Question 14.1 of the AQA GCSE Computer Science June 2022 Paper 1 (8525/1B, the Python paper) gives you a Python program with five gaps in it. The program fills a two-dimensional array using a while loop inside a while loop. It is worth 4 marks.
We do not copy the exam paper here. Open it beside this page: AQA June 2022 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
A bingo ticket is a 3 by 3 grid of computer science key terms. It is stored in a two-dimensional array called ticket, which starts full of empty strings. A subroutine called generateKeyTerm returns a random key term each time it is called.
The program sets i to 0. While i is less than 3 it sets j to something (gap 1). While j is less than 3 it stores a key term in ticket[ gap 2 ][ gap 3 ], then does something (gap 4). After the inner loop it does something else (gap 5).
Work it through
A for loop moves its own counter. A while loop does not: you have to set the counter up, and you have to move it on. Every while loop with a counter needs three things: a start, a test and a step. The tests are given. The gaps are the starts and the steps.
Gap 1 is 0. j counts the columns, and it must go back to 0 for every row. That is why this line is inside the outer loop.
Gaps 2 and 3 are i and j. i picks the row and j picks the column. The mark scheme accepts them the other way round too, because a 3 by 3 grid is filled either way.
Gap 4 is j = j + 1. It is inside the inner loop, so it is the inner loop's step.
Gap 5 is i = i + 1. It is lined up with the inner while, so it is inside the outer loop only. It is the outer loop's step.
The finished program
i = 0
while i < 3:
j = 0
while j < 3:
ticket[i][j] = generateKeyTerm()
j = j + 1
i = i + 1
One mark for starting j at 0, one for using i and j as the two index numbers, one for the j step and one for the i step. j += 1 is fine.
Where the marks are lost
- Swapping gaps 4 and 5. Use the indentation printed on the paper. The deeper gap belongs to the inner loop, so it moves
j. - Starting
jat 1. Indexing starts at 0. The front of the paper says so. i + 1with no assignment. That works out a number and throws it away. It must bei = i + 1.
It helps to ask what would go wrong. Without gap 4 the inner loop never ends. Without gap 1 only the first row is filled, because j is still 3 when the second row starts.
Run it
The finished program with a generateKeyTerm to call. It prints the ticket as a grid, and the robot's light goes green when the ticket is full.
The program
from bugbot import *
import random
connect()
TERMS = ["CPU", "ALU", "Pixel", "NOT gate", "Binary", "LAN", "Register", "Cache", "Protocol", "RAM", "Bit", "Router"]
def generateKeyTerm():
return TERMS[random.randrange(0, len(TERMS))]
ticket = [["", "", ""],
["", "", ""],
["", "", ""]]
i = 0
while i < 3:
j = 0
while j < 3:
ticket[i][j] = generateKeyTerm()
j = j + 1
i = i + 1
for row in ticket:
print(row)
led("green")
Now change it
Run it a few times and you will see the same term twice on one ticket. The question says a real ticket has nine different terms. Change the program so that a term is only stored if it is not on the ticket already. Which loop has to change, and why can it no longer step on every pass?
Answer
Keep a list of the terms used so far. In the inner loop, get a term and only store it (and only add 1 to j) if it is not in that list. If it is a repeat, go round again without moving j.Questions
What are the answers to AQA GCSE Computer Science 2022 Paper 1 Question 14.1?
The gaps are 0, then i and j as the two index numbers, then j = j + 1 inside the inner loop, then i = i + 1 inside the outer loop.
Why must j be set to 0 inside the outer loop?
Because the inner loop has to start again at the first column for every row. If j were set to 0 only once, it would still be 3 after the first row and the inner loop would not run again.
How do you loop over a two-dimensional array?
Use one loop inside another. The outer loop counts the rows and the inner loop counts the columns, and the element is array[row][column].
More from this paper
Every AQA 8525 question we have worked
Learn it step by step
- F3.5 Two-dimensional arrays Strings, lists and records
- F2.6 Condition-controlled loops: while 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.