Hard5 marksStructured
Fundamentals of algorithmsGeneraltracealgorithmsnested loops

AQA GCSE · Question 10.1 · Fundamentals of algorithms

count i person j result

Figure 9 shows an algorithm, represented in pseudo-code, used to display students' test scores. The algorithm does not work as expected and the teacher wants to find the error.

The algorithm should display three test scores for each student:

  • Natalie has results of 78, 81 and 72
  • Alex has results of 27, 51 and 54
  • Roshana has results of 52, 55 and 59.

Figure 9
1 names ← ['Natalie', 'Alex', 'Roshana']
2 scores ← [78, 81, 72, 27, 51, 54, 52, 55, 59]
3 count ← 0
4 FOR i ← 0 TO 2
5 person ← names[i]
6 OUTPUT 'Student: ', person
7 FOR j ← 0 TO 1
8 OUTPUT j + 1
9 result ← scores[i * 3 + j]
10 OUTPUT result
11 count ← count + 1
12 ENDFOR
13 ENDFOR
Complete the trace table for the algorithm shown in Figure 9. You may not need to use all the rows in the table.

How to approach this question

Trace the nested loops carefully. The outer loop (`i`) iterates through the students. The inner loop (`j`) is supposed to iterate through their scores. - **Initial:** `count` = 0 - **Outer loop i=0 (Natalie):** - `person` = "Natalie" - **Inner loop j=0:** `result` = scores[0*3+0] = scores[0] = 78. `count` = 1. (Record this row) - **Inner loop j=1:** `result` = scores[0*3+1] = scores[1] = 81. `count` = 2. (Record this row) - Inner loop finishes as it only goes `TO 1`. - **Outer loop i=1 (Alex):** - `person` = "Alex" - **Inner loop j=0:** `result` = scores[1*3+0] = scores[3] = 27. `count` = 3. (Record this row) - **Inner loop j=1:** `result` = scores[1*3+1] = scores[4] = 51. `count` = 4. (Record this row) - Inner loop finishes. - **Outer loop i=2 (Roshana):** - `person` = "Roshana" - **Inner loop j=0:** `result` = scores[2*3+0] = scores[6] = 52. `count` = 5. (Record this row) - **Inner loop j=1:** `result` = scores[2*3+1] = scores[7] = 55. `count` = 6. (Record this row) - Inner loop finishes. - Outer loop finishes.

Full Answer

The completed trace table is: - Row 1: count=1, i=0, person="Natalie", j=0, result=78 - Row 2: count=2, i=0, person="Natalie", j=1, result=81 - Row 3: count=3, i=1, person="Alex", j=0, result=27 - Row 4: count=4, i=1, person="Alex", j=1, result=51 - Row 5: count=5, i=2, person="Roshana", j=0, result=52 - Row 6: count=6, i=2, person="Roshana", j=1, result=55
This question requires a careful trace of nested loops and array indexing. The error in the algorithm is that the inner loop (`FOR j ← 0 TO 1`) only runs for j=0 and j=1, so it only processes two scores for each student instead of three. The trace reveals this behaviour. The index into the `scores` array is calculated as `i * 3 + j`. - For Natalie (i=0): - j=0: index is `0*3+0=0` -> `scores[0]` is 78. - j=1: index is `0*3+1=1` -> `scores[1]` is 81. - For Alex (i=1): - j=0: index is `1*3+0=3` -> `scores[3]` is 27. - j=1: index is `1*3+1=4` -> `scores[4]` is 51. - For Roshana (i=2): - j=0: index is `2*3+0=6` -> `scores[6]` is 52. - j=1: index is `2*3+1=7` -> `scores[7]` is 55. The `count` variable is incremented in each iteration of the inner loop.

Common mistakes

✗ Making a calculation error in the index `i * 3 + j`.\n✗ Forgetting that the inner loop only runs for j=0 and j=1.\n✗ Not updating all variables in the table for each step.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam