Complete the trace table for the algorithm shown in Figure 9 - AQA - GCSE Computer Science - Question 10 - 2023 - Paper 1
Question 10
Complete the trace table for the algorithm shown in Figure 9.
You may not need to use all the rows in the table.
count i person j result
0 0 Natalie 0 78
1... show full transcript
Worked Solution & Example Answer:Complete the trace table for the algorithm shown in Figure 9 - AQA - GCSE Computer Science - Question 10 - 2023 - Paper 1
Step 1
Complete the trace table for the algorithm shown in Figure 9.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To complete the trace table, we will analyze how the algorithm iterates through the names and scores, updating the count, i, person, j, and result columns accordingly.
The algorithm initializes count to 0.
For i = 0, person is 'Natalie'.
When j = 0:
Calculate result: scores[0 * 3 + 0] which gives 78.
Increment count to 1.
When j = 1:
Calculate result: scores[0 * 3 + 1] which gives 81.
Increment count to 1.
For i = 1, person is 'Alex'.
When j = 0:
Calculate result: scores[1 * 3 + 0] which gives 27.
Increment count to 2.
When j = 1:
Calculate result: scores[1 * 3 + 1] which gives 51.
Increment count to 2.
For i = 2, person is 'Roshana'.
When j = 0:
Calculate result: scores[2 * 3 + 0] which gives 55.
Increment count to 3.
When j = 1:
Calculate result: scores[2 * 3 + 1] which gives 55.
The trace table would look like the following:
count
i
person
j
result
0
0
Natalie
0
78
1
0
Natalie
1
81
1
1
Alex
0
27
2
1
Alex
1
51
2
2
Roshana
0
55
3
2
Roshana
1
55
Step 2
How could the error in the algorithm in Figure 9 be corrected?
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To correct the error in the algorithm: Change line number 10 to:
result <- scores[i * 3 + j]
This ensures that we correctly access the scores array using the indices i and j.