Explain how a compiler makes use of a stack when translating recursive programming code.
Was this exam question helpful?
Exam code: 9618
Explain how a compiler makes use of a stack when translating recursive programming code.
How did you do?
Was this exam question helpful?
The following recursive function is written in pseudocode. The line numbers are for reference only.
01 FUNCTION Total(N : INTEGER) RETURNS INTEGER
02 IF N = 1 THEN
03 RETURN 1
04 ELSE
05 RETURN N + Total(N - 1)
06 ENDIF
07 ENDFUNCTIONIdentify the line number of the base case and the line number of the general case in the function Total().
How did you do?
Was this exam question helpful?
The following function uses iteration to calculate the value of Base raised to the power Exponent. For example, Power(3, 4) returns 81.
Exponent is always 1 or greater.
FUNCTION Power(Base : INTEGER, Exponent : INTEGER) RETURNS INTEGER
DECLARE Result : INTEGER
DECLARE Count : INTEGER
Result ← 1
FOR Count ← 1 TO Exponent
Result ← Result * Base
NEXT Count
RETURN Result
ENDFUNCTIONWrite pseudocode for a recursive version of the function Power(). The function heading must stay the same.
How did you do?
Was this exam question helpful?
The following recursive function is written in pseudocode.
FUNCTION DigitSum(Number : INTEGER) RETURNS INTEGER
DECLARE Result : INTEGER
IF Number >= 10 THEN
Result ← (Number MOD 10) + DigitSum(Number DIV 10)
ELSE
Result ← Number
ENDIF
RETURN Result
ENDFUNCTIONIn this function, MOD returns the remainder after integer division. DIV returns the whole-number quotient.
(i) Complete the trace table for the function when it is called as DigitSum(4739). The first row has been completed for you. Rows where a call returns to an earlier call are shown with its call number in brackets.
Call number | Function call | Number | Result | Return value |
|---|---|---|---|---|
1 | DigitSum(4739) | 4739 | 9 + DigitSum(473) | |
(ii) State the purpose of the function DigitSum().
How did you do?
Was this exam question helpful?
A compiler translates a recursive function. When the program runs, the object code uses a stack to handle the recursive calls.
Name the process in which:
(i) the return address and the values of the local variables are pushed onto the stack with each recursive call, until the base case is reached
(ii) the return addresses and the values of the local variables are popped off the stack after the base case is reached.
How did you do?
Was this exam question helpful?
The following pseudocode shows a recursive function and a statement that calls it. The line numbers are for reference only.
01 FUNCTION Steps(N : INTEGER) RETURNS INTEGER
02 IF N = 1 THEN
03 RETURN 0
04 ELSE
05 RETURN 1 + Steps(N DIV 2)
06 ENDIF
07 ENDFUNCTION
...
20 Answer ← Steps(12)In this function, DIV returns the whole-number quotient.
Each time Steps() is called, the return address (the line number the call was made from) and the value of the parameter N are pushed onto a stack.
(i) Complete the table to show the contents of the stack at the moment the base case is reached. Row 1 is the top of the stack. You may not need all the rows.
Stack position | Return address | Value of N |
|---|---|---|
1 (top) | ||
2 | ||
3 | ||
4 | ||
5 |
(ii) State the value assigned to Answer on line 20.
How did you do?
Was this exam question helpful?
The following recursive procedure is written in pseudocode.
PROCEDURE Show(N : INTEGER)
IF N > 0 THEN
OUTPUT N
CALL Show(N - 2)
OUTPUT N * 10
ENDIF
ENDPROCEDUREThe procedure is called with the statement CALL Show(5)
(i) Write the output, in order, from this call.
(ii) The second group of values is output in the reverse order of the first. Explain, with reference to the stack, why this happens.
How did you do?
Was this exam question helpful?