9618/31/O/N/21 question 10: worked solution (October/November 2021, 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. Q3 9618/31/O/N/21 original Q10 Structured · 13 marks Worked solution (a) Write any three of these: There must be a base case (stopping condition) [1] There must be a general case [1] ...which calls itself (the function is defined in terms of itself) [1] Other points that also score: each call changes the state so it moves towards the base case; unwinding can happen once the base case is reached. (b) Write any three of these: A stack is a LIFO (last in, first out) structure [1] Each recursive call is pushed onto the stack [1] ...and is popped off when the function ends [1] Other points that also score: this allows backtracking / unwinding to keep the right order. (c) Write any two of these three: Queue [1] Linked list [1] Other points that also score: Binary tree . (d) Call StackFull(). If the stack is full, return a message. Otherwise add the integer and return a message. FUNCTION AddInteger(NewInteger : INTEGER) RETURNS STRING IF StackFull() THEN RETURN "The stack is full" ELSE TopOfStack ← TopOfStack + 1 ArrayStack[TopOfStack] ← NewInteger RETURN "Item added" ENDIF ENDFUNCTION Any five of these seven points score (any wording is fine for the messages): Test the stack with IF ... THEN ... ELSE ... ENDIF [1] Use StackFull() correctly in the test [1] Return a suitable message when the stack is full [1] Return a message when there is space [1] Add 1 to TopOfStack when there is space [1] Other points that also score: use NewInteger as the data stored; store it in the correct element, ArrayStack[TopOfStack]. KEY IDEA Recursion needs a base case and a self-calling general case, and the stack stores each call so it can unwind in reverse order. TOTAL 13 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by qiphyedu. Tap the picture to open it on YouTube.