9618/31/M/J/23 question 12: worked solution (May/June 2023, paper 31)

Show as plain text

Cambridge A2 Level Computer Science 9618 · 19.2 Recursion Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 2 of 2. Q25 9618/31/M/J/23 original Q12 Structured · 7 marks Worked solution (a) Write any two of these: Recursion is a process where a function or procedure is defined in terms of itself (it calls itself ) [1] It needs a base case (a way to return without making a recursive call) [1] Other points that also score: there must also be a general case where the recursive call happens. (b) Calls go down until Number is 0 or 1 (the base case), then answers are added back up ( unwinding ). Call number Function call Number Result Return value 1 Fib(5) 5 Fib(4) + Fib(3) 2 Fib(4) 4 Fib(3) + Fib(2) 3 Fib(3) 3 Fib(2) + Fib(1) 4 Fib(2) 2 Fib(1) + Fib(0) 5 Fib(1) 1 1 1 6 Fib(0) 0 0 0 (4) Fib(2) 2 1 + 0 1 (3) Fib(3) 3 1 + 1 2 (2) Fib(4) 4 2 + 1 3 (1) Fib(5) 5 3 + 2 5 Call number column correct [1] Function call and Number columns correct [1] Result column correct going down to the base case (rows 1 to 6) [1] Result column correct coming back up (rows 7 to 10) [1] Return value column correct, ending with 5 [1] KEY IDEA A recursive function stops at a base case, then each earlier call finishes in reverse order using the returned values. TOTAL 7 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by qiphyedu. Tap the picture to open it on YouTube.