Question 5

OCR Computer Science June 2017 Paper 2A level10 marks

On the specification

(a)
2.2 Problem solving and programming
(b)
2.2 Problem solving and programming
(c)
2.2 Problem solving and programming
Question text

5 A recursive function, calculate, is shown below: 1 function calculate(num1, num2) 2 if num1 == num2 then 3 return num1 4 elseif num1 < num2 then 5 return calculate(num1, (num2-num1)) 6 else 7 return calculate(num2, (num1-num2)) 8 endif 9 endfunction (a) Identify the lines where recursion is used. [1] (b) Trace the algorithm, showing the steps and result when the following line is run: print(calculate(4,10)) [5] (c) Re-write the function so it uses iteration instead of recursion. [4]