AQA GCSE Computer Science June 2023 Paper 1, Question 10: the nested FOR loop trace table
AQA 8525 June 2023 Paper 1, Question 10: trace a FOR loop inside a FOR loop that reads test scores from a list, then choose the fix for its error. The full trace table, the five marks explained, and the program to run.
Question 10 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) is the biggest trace table on the paper: five columns, 5 marks. Part 2 then asks you to choose the change that fixes the algorithm (1 mark). Do the trace carefully and Part 2 answers itself.
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
There are three students, and each has three test scores. The names are in one list. All nine scores are in a second list, one after another: the first student's three, then the second student's three, then the third's.
The algorithm should display three scores for each student. It does not, and the teacher wants to know why.
countstarts at 0.- An outer loop runs
ifrom 0 to 2. It storesnames[i]inpersonand outputs it. - An inner loop runs
jfrom 0 to 1. It outputsj + 1, storesscores[i * 3 + j]inresult, outputs that, and adds 1 tocount.
The trace table has the columns count, i, person, j and result.
Two things to get straight first
A pseudo-code FOR loop includes its end value. FOR j ← 0 TO 1 runs with j = 0 and then j = 1. Twice, not once. (In Python that is range(2).)
The index i * 3 + j jumps in threes. Each student owns three places in the list. i * 3 finds where that student's scores start: 0, 3 or 6. Then j steps along from there.
Work it through
Before the loops. count is 0.
i = 0. person is Natalie. With j = 0 the index is 0, so result is 78, and count becomes 1. With j = 1 the index is 1, so result is 81, and count becomes 2.
i = 1. person is Alex. With j = 0 the index is 3, so result is 27, and count becomes 3. With j = 1 the index is 4, so result is 51, and count becomes 4.
i = 2. person is Roshana. With j = 0 the index is 6, so result is 52, and count becomes 5. With j = 1 the index is 7, so result is 55, and count becomes 6.
The finished trace table
| count | i | person | j | result |
|---|---|---|---|---|
| 0 | 0 | Natalie | 0 | 78 |
| 1 | 1 | 81 | ||
| 2 | 1 | Alex | 0 | 27 |
| 3 | 1 | 51 | ||
| 4 | 2 | Roshana | 0 | 52 |
| 5 | 1 | 55 | ||
| 6 |
count has seven values because it starts at 0 and then goes up six times.
Where the five marks are
One mark each for:
- the whole
countcolumn; - the whole
icolumn; - the first Natalie row:
j= 0 andresult= 78; - the second Natalie row:
j= 1 andresult= 81; - all of the Alex and Roshana rows.
So two of the five marks are for columns read downwards, and the mark scheme accepts values on different rows so long as the order down each column is clear. It also ignores quotation marks round the names and small spelling slips in them.
Where the marks are lost
- Tracing what the algorithm should do. The third scores (72, 54 and 59) never appear. If they are in your table, you traced the description and not the code.
- Running the inner loop once.
0 TO 1is two passes. - Putting
j + 1in thejcolumn.OUTPUT j + 1prints 1 and 2, but it does not changej. Thejcolumn holds 0 and 1. - Stopping
countat 5. The last thing the algorithm does is add 1 tocountfor the sixth time.
Part 2: fix it
Your table shows the fault. Each student got two scores, not three, because the inner loop stops at 1. The fix is the option that changes line 7 so that the inner loop runs j from 0 to 2.
Check the other options against your trace. Starting count at -1 changes nothing that is displayed. Running i from 1 to 4 would go past the end of the names. Swapping i and j in the index would mix the students' scores up.
Run it
The same algorithm in Python. It prints one row of the trace table for every pass of the inner loop. PASSES is the number of times the inner loop runs: 2 is the broken version from the paper.
The program
from bugbot import *
connect()
# change PASSES to 3 to fix the algorithm
PASSES = 2
names = ["Natalie", "Alex", "Roshana"]
scores = [78, 81, 72, 27, 51, 54, 52, 55, 59]
count = 0
print("count i person j result")
for i in range(3):
person = names[i]
for j in range(PASSES):
result = scores[i * 3 + j]
print(count, i, person, j, result)
count = count + 1
print("count ends at", count)
Questions
What is the answer to AQA GCSE Computer Science 2023 Paper 1 Question 10.1?
The count column is 0 to 6. The i column is 0, 1, 2. The person column is Natalie, Alex, Roshana. For each person j is 0 then 1. The result column is 78, 81, 27, 51, 52, 55. The third score of each student never appears, which is the error.
What is the answer to Question 10.2?
C. Change line 7 so that the inner loop runs j from 0 to 2. It then reads all three scores for each student.
Does a FOR loop in AQA pseudo-code include the last value?
Yes. FOR j from 0 TO 2 runs with j as 0, 1 and 2. This is different from Python, where range(2) stops before 2.
How do I trace nested loops?
Finish every pass of the inner loop before you move the outer loop on. Write a new value only when a variable changes, and keep the order down each column correct.
More from this paper
- 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 12.1: Trace a binary search 4 marks
Every AQA 8525 question we have worked · Guide: Trace tables explained
Learn it step by step
- F13.2 Trace tables Exam preparation
- F2.7 Loop patterns Decisions and loops
- F6.4 Debugging logic errors Robust programs
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.