9618/22/O/N/24 question 3: worked solution (October/November 2024, paper 22)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 10.4 Introduction to Abstract Data Types (ADT) Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4. Q80 9618/22/O/N/24 original Q3 Structured · 7 marks Worked solution (a)(i) The stack grows upwards from index 1 and nothing is on it at the start. Write: SP = 1 and OnStack = 0 [1] (a)(ii) 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] (b) 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 ENDFUNCTION First 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] KEY IDEA A stack pointer shows the next free place: store at SP, then move SP up, and refuse to push when the stack is full. TOTAL 7 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.