IBDP Computer Science B2.2 Data structures SL Paper 1 - New Syllabus

Question 

Consider the following integer array, NUM:
 [0][1][2][3][4]
NUM71596
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:

KNUM[0]NUM[1]NUM[2]NUM[3]NUM[4]
071596
178596
2781396
37813226
478132228
4358132228

Working:

Initially:

K = 0
NUM = [7, 1, 5, 9, 6]

For K = 1:

NUM[1] = NUM[0] + NUM[1]
       = 7 + 1
       = 8

For K = 2:

NUM[2] = NUM[1] + NUM[2]
       = 8 + 5
       = 13

For K = 3:

NUM[3] = NUM[2] + NUM[3]
       = 13 + 9
       = 22

For K = 4:

NUM[4] = NUM[3] + NUM[4]
       = 22 + 6
       = 28

After the loop, K = 4, so:

NUM[0] = NUM[4] + NUM[0]
       = 28 + 7
       = 35

Therefore, the final value of NUM[0] is 35.

Scroll to Top