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

Recursive Function Analysis and Comparison

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)
17 views14 pages

Recursive Function Analysis and Comparison

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

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 ([Link])

Common questions

Powered by AI

The IsOdious function uses recursion by dividing the number by two (using DIV), effectively reducing the problem size with each call until the base case is encountered (n = 0). Each call determines whether the number of 1’s in its binary representation is odd or even, using recursive calls to evaluate smaller instances of the same type of problem .

The board game algorithm uses recursion to handle the addition of obstacles by recursively calling itself until it adds all the necessary obstacles determined by the dice roll. A potential issue without converting to iteration is the inefficiency and increased risk of stack overflow from too many recursive calls, especially if the board size or number of obstacles is large .

An example is the Tower of Hanoi problem, where the recursive approach naturally reflects the problem's structure: move n-1 disks from source to auxiliary peg, the nth to target, and finally n-1 disks from auxiliary to target. This recursion directly maps to the conceptual steps required, while an iterative solution would require complex loop structures and state management, making it less intuitive and harder to follow .

The primary challenge when converting a recursive function to an iterative one involves explicitly managing a stack data structure that mimics the program call stack used in recursive calls. This can become complex and error-prone, as it requires manually tracking the state of each level of recursion and ensuring that conditions are met to push and pop frames correctly, which can be naturally managed in recursion by the system stack .

A benefit of using iteration over recursion is that it avoids the overhead of multiple function calls and reduces the risk of stack overflow, leading to better space efficiency. A drawback is that iterative solutions can be less intuitive and harder to understand, particularly for problems that fit naturally into a recursive paradigm, such as the Euclidean algorithm for GCD .

The subroutine's purpose is to ensure that obstacles are not placed on already occupied spaces, thereby maintaining the game's integrity and preventing errors in gameplay. It checks if the randomly generated position is free by examining the board's current state and either places an obstacle at a free location or skips the position if occupied, enhancing the game's logic robustness .

An advantage of using recursion is the ability to express solutions to problems that have a natural recursive structure, such as tree traversal or factorial calculation, in a more straightforward and intuitive way. A disadvantage is the potential for stack overflow due to deep recursion and generally higher memory usage because of the stored call stack frames .

Rewriting 'thisFunction' using iteration involves using a loop to repeatedly adjust the search bounds instead of recursive calls: 01 function iterativeThisFunction(theArray, num1, num2, num3) 02 while num1 <= num2 do 03 result = num1 + ((num2 - num1) DIV 2) 04 if theArray[result] < num3 then 05 num1 = result + 1 06 elseif theArray[result] > num3 then 07 num2 = result - 1 08 else 09 return result 10 endif 11 endwhile 12 return -1 13 endfunction Challenges include ensuring correct maintenance of loop invariants and conditions to prevent infinite loops .

The function 'thisFunction' performs a binary search, which is indicated by the way it narrows the search range by adjusting num1 and num2 until it finds or decides the item is not in the array. When the function is called with the arguments (theArray, 0, 7, 35), it returns the index 6. This is because theArray[6] is 35, which matches the num3 parameter passed to the function .

One difference is that recursion involves function calling itself with new parameters, which can be more intuitive for problems naturally described in recursive form (e.g., factorial, binary search), while iteration uses constructs like loops to repeat operations. Another difference is that recursion uses more memory on the call stack, which could lead to stack overflow for deep recursive calls, whereas iteration typically uses a fixed amount of memory regardless of the number of iterations .

You might also like