Recursion 4
Reinas
Task 1
1.
a) Briefly explain the main features of a recursive procedure from the programmer’s point
of view. Explain what is required from the system to enable recursion to be used.
The recursive procedure must have a valid input into the procedure and the procedure
must return a valid back into self by calling the function from within the procedure.
[3]
b) The following recursive subroutine carries out a list operation.
procedure processList(numList)
if length(numList) > 0 then
remove first element of numList and store in first
processList(numList)
append first to end of numList
endif
return numList
endprocedure
i. Complete the following trace table if the list numbers is defined in the main program
as numbers = [3,5,10,2] and the subroutine is called with the statement
new = processList(numbers)
numlist
Length (numlist) 0 1 2 3 first new
4 3 5 10 2 3 [3,5,10,2]
3 5 10 2 5 [5,10,2]
2 10 2 10 [10,2]
1 2 2 [2]
0 []
1 2 2 [2]
2 2 10 10 [2,10]
3 2 10 5 5 [2,10,5]
4 2 10 5 3 3 [2,10,5,3]
[6]
ii. Explain what the subroutine does
The sub routine reverses the order of the list.
[1]
1Reinas<#>