0% found this document useful (0 votes)
52 views14 pages

Recursion

The document presents a recursive function 'thisFunction()' that performs a binary search on an array to find a specific number. It includes a call to this function with specific parameters and asks for the final return value, the name of the algorithm, and comparisons between recursion and iteration. Additionally, it discusses other recursive functions, their iterative counterparts, and the advantages and disadvantages of using recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views14 pages

Recursion

The document presents a recursive function 'thisFunction()' that performs a binary search on an array to find a specific number. It includes a call to this function with specific parameters and asks for the final return value, the name of the algorithm, and comparisons between recursion and iteration. Additionally, it discusses other recursive functions, their iterative counterparts, and the advantages and disadvantages of using recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1(a) Hugh has written a recursive function called thisFunction() using pseudocode.

01 function thisFunction(theArray, num1, num2, num3)


02 result = num1 + ((num2 - num1) DIV 2)
03 if num2 < num1 then
04 return -1
05 else
06 if theArray[result] < num3 then
07 return thisFunction(theArray, result + 1, num2, num3)
08 elseif theArray[result] > num3 then
09 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

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.

© OCR 2025. You may photocopy this page. 1 of 14 Created in ExamBuilder


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.

[4]

© OCR 2025. You may photocopy this page. 2 of 14 Created in ExamBuilder


(d) The recursive function thisFunction() is printed again here for your reference.

01 function thisFunction(theArray, num1, num2, num3)


02 result = num1 + ((num2 - num1) DIV 2)
03 if num2 < num1 then
04 return -1
05 else
06 if theArray[result] < num3 then
07 return thisFunction(theArray, result + 1, num2, num3)
08 elseif theArray[result] > num3 then
09 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.

© OCR 2025. You may photocopy this page. 3 of 14 Created in ExamBuilder


[6]

2 A recursive function, GCD, is given in pseudocode.

function GCD (num1, num2)

if num2 == 0 then

return num1

else

return GCD(num2, num1 MOD num2)

endif

endfunction

The function has been rewritten using iteration instead of recursion.

i. State one benefit and one drawback of using iteration instead of recursion.

Benefit

Drawback

[2]

ii. Complete the missing statements in this iterative version of the function.

function newGCD (num1, num2)

temp = 0

while (num2 != ...................)

© OCR 2025. You may photocopy this page. 4 of 14 Created in ExamBuilder


................... = num2

num2 = num1 MOD ...................

num1 = temp

endwhile

return ...................

endfunction

[4]

3(a) A recursive function, calculate, is shown below:

Identify the lines where recursion is used.

[1]

© OCR 2025. You may photocopy this page. 5 of 14 Created in ExamBuilder


(b) Re-write the function so it uses iteration instead of recursion.

[4]

© OCR 2025. You may photocopy this page. 6 of 14 Created in ExamBuilder


4(a) Some whole numbers are known by mathematicians as evil numbers.
One way to find out if a number is evil, is to use the integer division operators DIV and MOD.

Describe how recursion has been used in this function.

[2]

(b) All numbers that are not evil are known as odious numbers. The following function determines whether a number
is odious.

1 FUNCTION IsOdious(n : INTEGER)

2 IF n = 0 THEN

3 RETURN FALSE

4 ELSE

5 IF n MOD 2 = 0 THEN

6 RETURN IsOdious(n DIV 2)

7 ELSE

8 RETURN NOT(IsOdious(n DIV 2))

9 END IF

© OCR 2025. You may photocopy this page. 7 of 14 Created in ExamBuilder


10 END IF

11 END FUNCTION

2 is an odious number.

Show each step of the execution of the call IsOdious(2), including all recursive calls and the values returned.
You may use a diagram.

© OCR 2025. You may photocopy this page. 8 of 14 Created in ExamBuilder


[6]

© OCR 2025. You may photocopy this page. 9 of 14 Created in ExamBuilder


(c) Many functions can be defined using either recursion or iteration.

i. State one advantage of using recursion instead of iteration.

ii. State one disadvantage of using recursion instead of iteration.

[2]

© OCR 2025. You may photocopy this page. 10 of 14 Created in ExamBuilder


5 The layout for a 2-player board game is shown in Fig 2.1

The game is played by rolling two 6-sided dice and moving that number of spaces. Both players start on the
START space. If a player lands on a space occupied by the other player, they move to the next available space.

The board is to be stored as a 2-dimensional array.

Each time a player moves, a series of obstacles are to be added to the board.

On their turn, each player rolls two dice. The smaller number from the two dice is taken, and that many obstacles
will appear on the board in random locations.

For example, if a 3 and 6 are rolled, then 3 obstacles will appear.

A recursive function is written in pseudocode to perform this task.

© OCR 2025. You may photocopy this page. 11 of 14 Created in ExamBuilder


The code generates an instance of the object obstacle.

i. Explain the purpose of the code in line in the algorithm.

[2]

ii. Identify the line of code where recursion occurs.

[1]

iii. The recursive function could have been written using iteration.

Describe the benefits and drawbacks of using recursion instead of iteration.

Benefits

Drawbacks

[4]

iv. Rewrite the function so it uses iteration instead of recursion.

© OCR 2025. You may photocopy this page. 12 of 14 Created in ExamBuilder


[4]

v. If a position on the board is not occupied, its value is set to a blank string ("").

The current algorithm does not check if the random space generated is currently occupied.

Write a subroutine that takes the generated position of the board, checks if it is free and returns if free,
or if occupied.

[3]

© OCR 2025. You may photocopy this page. 13 of 14 Created in ExamBuilder


6 Consider the following algorithm in Fig.2, expressed in pseudocode, as a function S:

i. Describe what is meant by recursion.

[2]

ii. Identify one example of where recursion occurs in this algorithm.

[1]

END OF QUESTION PAPER

© OCR 2025. You may photocopy this page. 14 of 14 Created in ExamBuilder

Powered by TCPDF (www.tcpdf.org)

You might also like