Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q12 · 9618/32/O/N/23 Q9

Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.

Q129618/32/O/N/23original Q9Structured · 13 marks
Question 9 from 9618/32/O/N/23

Worked solution

(a)(i)

Read the declarations in part (a)(ii) to find each type and purpose. Write:

IdentifierData typeDescription
BasePointerINTEGERPoints to the bottom of the stack
TopPointerINTEGERPoints to the top of the stack
StackREALList of decimal numbers stored in the stack

All five empty boxes correct [2]

Three or four of the empty boxes correct scores [1] instead.

(a)(ii)

Initialise sets the bottom pointer to 1 and the top pointer to 0 (empty stack). Push only adds if the stack is not full, then moves the top pointer up and stores the item.

PROCEDURE Initialise()
    BasePointer ← 1
    TopPointer ← 0
ENDPROCEDURE

PROCEDURE Push(NewItem : REAL)
    IF TopPointer < MaxSize THEN
        TopPointer ← TopPointer + 1
        Stack[TopPointer] ← NewItem
    ENDIF
ENDPROCEDURE

BasePointer ← 1 [1]

TopPointer ← 0 [1]

IF TopPointer < MaxSize THEN [1]

TopPointer ← TopPointer + 1 [1]

Stack[TopPointer] ← NewItem [1]

(b)

Write one point about the linked list and one about the array.

A linked list is a dynamic data structure, so it is not restricted in size and can grow or shrink by adding or removing nodes [1]

An array is a static data structure of fixed size, so when it is full the stack cannot be extended [1]

Another point for the linked list: editing is more efficient because pointers change instead of moving data.

(c)

Think about what the compiler must make the program do when a function calls itself.

The compiler produces object code that uses a stack [1]

Each recursive call pushes the return address and the values of local variables onto the stack (this is the winding) [1]

The return addresses and local values are popped off the stack [1]

This happens once the base case is reached, and it is the unwinding: the program returns through each call in turn [1]

KEY IDEAA stack remembers the return address and local values of each recursive call, so the calls can be unwound in reverse order.
TOTAL 13 marks

Official mark scheme

Mark scheme for 9618/32/O/N/23 question 9
VIDEO
▶ Whole paper

Video solution of the whole paper · by TEACHING BY SM. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.