Question 3
OCR Computer Science November 2020 Paper 2A level16 marks
On the specification
- (a)
- 2.3 Algorithms
- (b)
- 2.3 Algorithms
- (c)
- 2.2 Problem solving and programming
- (d)
- 2.2 Problem solving and programming
Question text
3 Hugh has written a recursive function called thisFunction() using pseudocode. 1 function thisFunction(theArray, num1, num2, num3) 2 result = num1 + ((num2 - num1) DIV 2) 3 if num2 < num1 then 4 return -1 5 else 6 if theArray[result] < num3 then 7 return thisFunction(theArray, result + 1, num2, num3) 8 elseif theArray[result] > num3 then 9 return thisFunction(theArray, num1, result - 1, num3) 10 else 11 return result 12 endif 13 endif 14 endfunction The function DIV calculates integer division, e.g. 5 DIV 3 = 1 (a) theArray has the following data: Index: 0 1 2 3 4 5 6 7 Data: 5 10 15 20 25 30 35 40 Trace the algorithm, and give the final return value, when it is called with the following statement: thisFunction(theArray, 0, 7, 35) You may choose to use the table below to give your answer. Function call num1 num2 num3 result thisFunction(theArray,0,7,35) Final return value [5] (b) State the name of the standard algorithm thisFunction() performs. [1] (c) Hugh could have written thisFunction() using iteration instead of recursion. Compare two differences between recursion and iteration. 1 2 [4] (d) The recursive function thisFunction() is printed again here for your reference. 1 function thisFunction(theArray, num1, num2, num3) 2 result = num1 + ((num2 - num1) DIV 2) 3 if num2 < num1 then 4 return -1 5 else 6 if theArray[result] < num3 then 7 return thisFunction(theArray, result + 1, num2, num3) 8 elseif theArray[result] > num3 then 9 return thisFunction(theArray, num1, result - 1, num3) 10 else 11 return result 12 endif 13 endif 14 endfunction Rewrite the function thisFunction() so that it uses iteration instead of recursion. You should write your answer using pseudocode or program code. [6]