AQA GCSE Computer Science June 2023 Paper 1, Question 14: completing a two-dimensional array algorithm
AQA 8525 June 2023 Paper 1, Question 14: fill three gaps in a pseudo-code subroutine that finds the highest vote in a two-dimensional array. Each gap explained, with the finished algorithm in Python to run and extend.
Question 14 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) shows an unfinished pseudo-code subroutine with three parts missing, labelled L1 to L3. This time there is no grid of options: you have to write the missing parts yourself. One mark each, 3 marks in all.
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
Fifty students voted for their favourite kind of music. The results are in a two-dimensional array called results with two rows:
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| row 0 | Pop | Post-Punk | Techno | Metal | Dance |
| row 1 | '7' | '19' | '14' | '1' | '9' |
Row 0 holds the names. Row 1 holds the votes, stored as text.
A subroutine, showResults(method, numberOfGenres), should output the genre with the highest vote. It:
- sets
posto 0 andhighto -1; - if
methodis'HIGHEST', loopsiover the genres. It turnsresults[L1][i]into an integer calledvotes. Ifvotesbeatshigh, it storesvotesinhighand stores L2 inpos; - otherwise outputs "not yet working";
- at the end, outputs the genre and the votes at position
pos.
The main program asks the user for HIGHEST or LOWEST, stores the answer in method, and calls showResults(L3, 5).
Work it through
L1 is 1. The loop is reading votes, because it converts what it reads into an integer. The votes are in row 1. With a two-dimensional array written results[row][column], the row comes first.
L2 is i. When a new highest vote is found, the algorithm must remember where it was, so that it can output the matching name afterwards. The place is the loop counter, i. Look at the last output line for the proof: it uses results[0][pos] and results[1][pos], so pos is a column number.
L3 is method. The subroutine has two parameters. The second argument in the call is 5, the number of genres. The first must be the user's choice, which the line above stored in method.
The answers
| Label | Answer |
|---|---|
| L1 | 1 |
| L2 | i |
| L3 | method |
Where the marks are lost
0for L1. Row 0 holds the names. Turning "Pop" into an integer would crash.votesfor L2. That would store 19 inpos, and there is no column 19.highremembers the value.posremembers the place.'HIGHEST'for L3. That would work for one choice only, and ignore what the user typed. Pass the variable.USERINPUTfor L3. The input has already been read intomethodon the line before.
Run it
The same subroutine in Python. A two-dimensional array in Python is a list of lists. It prints each vote as it compares it.
The program
from bugbot import *
connect()
# HIGHEST works. LOWEST is for you to write
METHOD = "HIGHEST"
def show_results(method, number_of_genres):
results = [["Pop", "Post-Punk", "Techno", "Metal", "Dance"],
["7", "19", "14", "1", "9"]]
pos = 0
high = -1
if method == "HIGHEST":
for i in range(number_of_genres):
votes = int(results[1][i]) # L1
print(" ", results[0][i], votes)
if votes > high:
high = votes
pos = i # L2
else:
print("not yet working")
if high != -1:
print(results[0][pos], "with", results[1][pos])
show_results(METHOD, 5) # L3 is the variable
Now change it
Finish the job the paper left undone. Make LOWEST work, so that it prints Metal with 1. You will need a second variable like high. What should it start at, given that there were 50 students?
Answer
Start a variable called low at 51, which is more than any genre can have. Loop in the same way, and when votes is less than low, store votes in low and i in pos. Change the last test so that it outputs when either search has run.Questions
What are the answers to AQA GCSE Computer Science 2023 Paper 1 Question 14?
L1 is 1, because the votes are in row 1 of the array. L2 is i, the position of the highest vote so far. L3 is method, the variable holding the user's choice.
How do you index a two-dimensional array?
Give two index numbers. In AQA pseudo-code and in Python, results[1][3] means row 1, column 3. Both start counting at 0.
Why does the algorithm start high at -1?
So that the first real vote is certain to beat it. No genre can have fewer than 0 votes, so -1 is lower than anything in the data. It also lets the algorithm tell whether a search has run at all.
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
- F4.2 Parameters and return values Functions and structured code
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.