IBDP Computer Science B2.2 Data structures SL Paper 1 - New Syllabus
Question
Consider the following integer array,
NUM:| [0] | [1] | [2] | [3] | [4] | |
|---|---|---|---|---|---|
| NUM | 7 | 1 | 5 | 9 | 6 |
and the following algorithm:
K = 0
S = 0
loop while K < 4
K = K + 1
NUM[K] = NUM[K-1] + NUM[K]
end loop
NUM[0] = NUM[K] + NUM[0]Copy and complete the following trace table: [5]
Most-appropriate topic code (CED):
• TOPIC B2.4: Programming algorithms
• TOPIC B2.2: Data structures
▶️ Answer/Explanation
Completed trace table:
| K | NUM[0] | NUM[1] | NUM[2] | NUM[3] | NUM[4] |
|---|---|---|---|---|---|
| 0 | 7 | 1 | 5 | 9 | 6 |
| 1 | 7 | 8 | 5 | 9 | 6 |
| 2 | 7 | 8 | 13 | 9 | 6 |
| 3 | 7 | 8 | 13 | 22 | 6 |
| 4 | 7 | 8 | 13 | 22 | 28 |
| 4 | 35 | 8 | 13 | 22 | 28 |
Working:
Initially:
K = 0
NUM = [7, 1, 5, 9, 6]For K = 1:
NUM[1] = NUM[0] + NUM[1]
= 7 + 1
= 8For K = 2:
NUM[2] = NUM[1] + NUM[2]
= 8 + 5
= 13For K = 3:
NUM[3] = NUM[2] + NUM[3]
= 13 + 9
= 22For K = 4:
NUM[4] = NUM[3] + NUM[4]
= 22 + 6
= 28After the loop, K = 4, so:
NUM[0] = NUM[4] + NUM[0]
= 28 + 7
= 35Therefore, the final value of NUM[0] is 35.
