Unlock access to all the studying documents.
View Full Document
Copyright Cengage Learning. Powered by Cognero.
1. The following is a valid recursive definition to determine the factorial of a non-negative integer.
0! = 1
1! = 1
n! = n * (n – 1)! if n > 0
2. In a recursive function, the base case stops the recursion.
3. With recursion, the base case must eventually be reduced to a general case.
4. The following is an example of a recursive function.
void print(int x)
{
if (x > 0)
{
cout << x << ” ” ;
print (x – 1);
Copyright Cengage Learning. Powered by Cognero.
5. Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters.
6. Infinite recursions execute forever on a computer.
7. You can use a recursive algorithm to find the largest element in an array.
a.
Statements in Lines 3 and 4
b.
Statements in Lines 5 and 6
c.
Statements in Lines 3-6
d.
Statements in Lines 5-10
ANSWER:
Statements in Lines 3 and 4.
Statements in Lines 5 and 6.
Statements in Lines 3, 4, and 5.
12. Consider the accompanying definition of a recursive function. Which of the statements represent the general case?
Statements in Lines 3 and 4
Statements in Lines 4, 5, and 6
Statements in Lines 5 and 6
Copyright Cengage Learning. Powered by Cognero.
cout << recFunc(10) << endl;
17. Consider the following definition of the recursive function print.
void print(int num)
{
if (num > 0)
{
cout << num << ” “;
print(num – 1);
}
}
What is the output of the following statement?
print(4);
18. Tracing through ____ recursion is more tedious than tracing other recursive forms.
Copyright Cengage Learning. Powered by Cognero.
22. Consider the accompanying definition of the recursive function mystery. Given the declaration:
int alpha[5] = {1, 4, 5, 8, 9};
what is the output of the following statement?
cout << mystery(alpha, 0, 4) << endl;
23. Consider the following definition of the recursive function mystery.
int mystery(int first, int last)
{
if (first > last)
return 0;
else if (first == last)
return first;
else
return first + mystery(first + 1, last – 1);
}
What is the output of the following statement?
cout << mystery(6, 10) << endl;
24. Consider the following definition of the recursive function mystery.
int mystery(int num)
{
a.
b.
c.
5760
d.
10800
ANSWER:
POINTS:
REFERENCES:
1043-1044
QUESTION TYPE:
Multiple Choice
HAS VARIABLES:
PREFACE NAME:
puzzle
DATE CREATED:
10/5/2016 1:43 PM
DATE MODIFIED:
10/5/2016 1:43 PM
if (num <= 0)
return 0;
else if (num % 2 == 0)
return num + mystery(num – 1);
else
return num * mystery(num – 1);
}
What is the output of the following statement?
cout << mystery(5) << endl;
Copyright Cengage Learning. Powered by Cognero.
cout << puzzle(3, 7) << endl;
27. Which of the following function headings can be used for a recursive definition of a function to calculate the nth
Fibonacci number?
void rFibNum(int a, int b)
bool rFibNum(int a, int b)
bool rFibNum(int a, int b, int n)
int rFibNum(int a, int b, int n)
28. How many needles are used in the Tower of Hanoi problem?
29. Which of the following rules should you follow to solve the Tower of Hanoi problem?
Only two disks can be moved at a time.
You can remove disks only from the first needle.
The removed disk must be placed on a smaller disk.
A smaller disk can be placed on top of a larger disk.
Copyright Cengage Learning. Powered by Cognero.
F(n) = F(n – 1) + 1 if n > 1
The value of F(3) is ____________________.
34. The recursive algorithm must have one or more base cases, and the general solution must eventually be reduced to
a(n) ____________________.
35. Recursive algorithms are implemented using ____________________ functions.
36. Consider the following code.
int fact(int num)
{
if (num == 0)
return 1;
else
return num * fact(num – 1);
}
The function fact is an example of a(n) ____________________ recursive function.
Copyright Cengage Learning. Powered by Cognero.
41. In the Tower of Hanoi problem, if needle 1 contains three disks, then the number of moves required to move all three
disks from needle 1 to needle 3 is ____________________.