9618/31/O/N/23 question 10: worked solution (October/November 2023, paper 31)

Show as plain text

Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2. Q10 9618/31/O/N/23 original Q10 Structured · 10 marks Worked solution (a) Declare the constant, two integers and the array: CONSTANT Capacity = 25 DECLARE BasePointer : INTEGER DECLARE TopPointer : INTEGER DECLARE Stack : ARRAY[1:25] OF REAL Constant declared correctly [1] Two INTEGER variables declared correctly [1] Array of 25 REAL items declared correctly [1] (b) The stack is empty when TopPointer is below BasePointer. Take the top item, then lower TopPointer. The function returns a REAL. FUNCTION Pop() RETURNS REAL DECLARE Item : REAL Item ← 0 IF TopPointer >= BasePointer THEN Item ← Stack[TopPointer] TopPointer ← TopPointer - 1 ELSE OUTPUT "The stack is empty - error" ENDIF RETURN Item ENDFUNCTION RETURNS REAL added to the header [1] IF TopPointer >= BasePointer THEN [1] Item ← Stack[TopPointer] [1] TopPointer ← TopPointer - 1 [1] RETURN Item [1] (c) Write any two of these points: A queue is FIFO (first in, first out) and a stack is LIFO (last in, first out) [1] Both can change in size [1] Other points that also score: a stack uses one end for push and pop, but a queue uses opposite ends to enqueue and dequeue (a stack has one moving pointer, a queue has two). KEY IDEA A stack pops from the top and must check it is not empty first, while a queue is first in, first out. TOTAL 10 marks Official mark scheme VIDEO ▶ Topic lesson A lesson on this topic · by rjmcompsci. Tap the picture to open it on YouTube.