Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.
Read the declarations in part (a)(ii) to find each type and purpose. Write:
| Identifier | Data type | Description |
|---|---|---|
| BasePointer | INTEGER | Points to the bottom of the stack |
| TopPointer | INTEGER | Points to the top of the stack |
| Stack | REAL | List of decimal numbers stored in the stack |
All five empty boxes correct [2]
Three or four of the empty boxes correct scores [1] instead.
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
ENDPROCEDUREBasePointer ← 1 [1]
TopPointer ← 0 [1]
IF TopPointer < MaxSize THEN [1]
TopPointer ← TopPointer + 1 [1]
Stack[TopPointer] ← NewItem [1]
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.
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]
▶ 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.