Professional Documents
Culture Documents
Chapter 25 Homework Questions: Mr. Mahmoud Moussa A2 CS 9618
Chapter 25 Homework Questions: Mr. Mahmoud Moussa A2 CS 9618
The following is a recursively defined function which calculates the result of BaseExponent. For
example, 23 is 8.
FUNCTION Power (Base: INTEGER, Exponent INTEGER) RETURNS INTEGER
IF Exponent = 0 THEN
Result ← 1
ELSE
Result ← Base * Power (Base, Exponent - 1)
ENDIF
RETURN Result
END FUNCTION
www.mahmoudmoussa.com 1
Mr. Mahmoud Moussa A2 CS 9618
Explain the role of the stack in the execution of the Power function. [2]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Write a pseudocode non-recursive (iterative) version of the Power function. [3]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
www.mahmoudmoussa.com 2
Mr. Mahmoud Moussa A2 CS 9618
Give one reason why a non-recursive Power function may be preferred to a recursive one. [1]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Give one reason why a recursive Power function may be preferred to a non- recursive one. [1]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
www.mahmoudmoussa.com 3
Mr. Mahmoud Moussa A2 CS 9618
Question 3
The following is a recursively defined function which calculates the nth integer in the sequence of
Fibonacci numbers
01 FUNCTION Fibonacci (n : INTEGER) RETURNS INTEGER
02 IF (n = 0) OR (n = 1 )
03 THEN
04 Result ← 1
05 ELSE
06 Result ← Fibonacci (n - 1) + Fibonacci (n - 2)
07 ENDIF
08 RETURN Result
09 ENDFUNCTION
www.mahmoudmoussa.com 4
Mr. Mahmoud Moussa A2 CS 9618
Dry run the procedure X by completing the trace table for the procedure call:
CALL X(40)
OUTPUT
…………………………………………………………………………………………………………
State the process that is carried out by procedure X. [1]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
www.mahmoudmoussa.com 5
Mr. Mahmoud Moussa A2 CS 9618
Question 5
An array MyList is used to store a sorted data set of non-zero integers. Unused cells contain zero.
Complete the trace table for the dry-run of the pseudocode for the procedure
CALL X(1, 9).
www.mahmoudmoussa.com 6
Mr. Mahmoud Moussa A2 CS 9618
State the purpose of procedure X when used with the array MyList. [2]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
www.mahmoudmoussa.com 7
Mr. Mahmoud Moussa A2 CS 9618
Question 6
NameList is a 1D array that stores a sorted list of names. A programmer declares the array in
pseudocode as follows:
NameList : Array[0 : 100] OF STRING
The programmer wants to search the list using a binary search algorithm.
The programmer decides to write the search algorithm as a recursive function. The function, Find,
The function will return the position of the name in the list, or −1 if the name is not found.
Complete the pseudocode for the recursive function.
FUNCTION Find(BYVAL Name : STRING, BYVAL Start : INTEGER, BYVAL
Finish : INTEGER) RETURNS INTEGER
// base case
IF ……………………………………………………………………………………………… THEN
RETURN -1
ELSE
Middle ← ………………………………………………………………………………………………
IF ………………………………………………………………………………………………THEN
RETURN ………………………………………………………………………………………………
ELSE // general case
IF SearchItem > ………………………………………………………………………………THEN
………………………………………………………………………………………………
ELSE
………………………………………………………………………………………………
ENDIF
ENDIF
ENDIF
ENDFUNCTION
www.mahmoudmoussa.com 8