Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.
The stack grows upwards from index 1 and nothing is on it at the start.
Write: SP = 1 and OnStack = 0 [1]
Say why unused elements are never read.
Values in unused elements can never be popped (taken off) [1]
Until a value has been pushed, which overwrites whatever was there [1]
The stack is full when OnStack is 60 (SP would be 61). Store the value at position SP, then move SP up by one.
FUNCTION Push(ThisValue : REAL) RETURNS BOOLEAN
DECLARE ReturnValue : BOOLEAN
IF OnStack = 60 THEN
RETURN FALSE // stack is already full
ENDIF
ThisStack[SP] ← ThisValue
SP ← SP + 1
OnStack ← OnStack + 1
RETURN TRUE
ENDFUNCTIONFirst gap: OnStack = 60 (or SP = 61, SP > 60, OnStack > 59) [1]
Second gap: FALSE [1]
Third gap: ThisStack[SP] [1]
Fourth gap: SP + 1 [1]
▶ Whole paper
Video solution of the whole paper · by Papersdock. 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.