0% found this document useful (0 votes)
60 views8 pages

Chapter 25 Homework Questions: Mr. Mahmoud Moussa A2 CS 9618

This document contains homework questions about recursion and recursive functions. It includes sample recursive functions for calculating exponents and Fibonacci numbers. It asks the student to explain recursion, describe how stacks are used in recursive function calls, provide pseudocode for iterative versions of recursive functions, and trace sample recursive functions. The purpose is to test the student's understanding of recursion and recursive function design and execution.

Uploaded by

Eyad Usama
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)
60 views8 pages

Chapter 25 Homework Questions: Mr. Mahmoud Moussa A2 CS 9618

This document contains homework questions about recursion and recursive functions. It includes sample recursive functions for calculating exponents and Fibonacci numbers. It asks the student to explain recursion, describe how stacks are used in recursive function calls, provide pseudocode for iterative versions of recursive functions, and trace sample recursive functions. The purpose is to test the student's understanding of recursion and recursive function design and execution.

Uploaded by

Eyad Usama
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/ 8

Mr.

Mahmoud Moussa A2 CS 9618

Chapter 25 Homework Questions


Question 1

Distinguish between iteration and recursion. [2]


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Give one advantage and one disadvantage of using recursive subroutines. [2]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Question 2

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

What is meant by 'recursively defined '? [1]


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………

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

Which line is the base case? [1]


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Which line is the general case? [1]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Question 4

A recursively defined procedure X is defined below:


PROCEDURE X(n : INTEGER)
IF (n = 0) OR (n = 1) THEN
OUTPUT n
ELSE
CALL X(n DIV 2)
OUTPUT (n MOD 2)
ENDIF
ENDPROCEDURE

Explain what is meant by recursively defined. [1]


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Explain how a stack is used during the execution of a recursive procedure. [2]
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………

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

State what is meant by a recursively defined procedure. [2]


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Write the line number from the pseudocode shown below that shows the procedure X is recursive.
[1]
…………………………………………………………………………………………………………
01 PROCEDURE X(Index, Item)
02 IF MyList[Index] > 0
03 THEN
04 IF MyList(Index) >= Item
05 THEN
06 MyList[Index] ← MyList[Index + 1]
07 ENDIF
08 CALL X(Index + 1, Item)
09 ENDIF
10 ENDPROCEDURE

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,

takes three parameters:


• Name, the string to be searched for

• Start, the index of the first item in the list to be searched

• Finish, the index of the last item in the list to be searched

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

You might also like