9618/32/O/N/23 question 9: worked solution (October/November 2023, paper 32)
Show as plain textHide 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. Q12 9618/32/O/N/23 original Q9 Structured · 13 marks Worked solution (a)(i) 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. (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 IDEA A 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 VIDEO ▶ Whole paper Video solution of the whole paper · by TEACHING BY SM. Tap the picture to open it on YouTube.