Quiz 8
int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << recFunc(10) << endl;
10
int mystery(int list[], int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } 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;
27
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);
4 3 2 1
int puzzle(int start, int end) { if (start > end) return start - end; else if (start == end) return start + end; else return end * puzzle(start + 1, end - 1); } Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << puzzle(3, 7) << endl;
420
Consider the following definition of the recursive function mystery. int mystery(int num) { 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;
50
int mystery(int list[], int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } Consider the accompanying definition of the recursive function mystery. Given the declaration: int beta[10] = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25}; What is the output of the following statement? cout << mystery(beta, 4, 7) << endl;
66
int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << recFunc(8) << endl;
720
____ control structures use a looping structure, such as while, for, or do...while, to repeat a set of statements.
Iterative
Which of the following solutions is easier to construct for the Tower of Hanoi problem?
Recursive
int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } //Line 7 Consider the accompanying definition of a recursive function. Which of the statements represents the base case?
Statements in Lines 3 and 4.
void printNum(int num) //Line 1 { //Line 2 if (n < 0) //Line 3 cout << "Num is negative" << endl; //Line 4 else if (num == 0) //Line 5 cout << "Num is zero" << endl; //Line 6 else //Line 7 { //Line 8 cout << num << " "; //Line 9 printNum(num - 1); //Line 10 } //Line 11 } //Line 12 Consider the accompanying definition of a recursive function. Which of the statements represent the base case?
Statements in Lines 3-6
int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } //Line 7 Consider the accompanying definition of a recursive function. Which of the statements represent the general case?
Statements in Lines 5 and 6
void printNum(int num) //Line 1 { //Line 2 if (n < 0) //Line 3 cout << "Num is negative" << endl; //Line 4 else if (num == 0) //Line 5 cout << "Num is zero" << endl; //Line 6 else //Line 7 { //Line 8 cout << num << " "; //Line 9 printNum(num - 1); //Line 10 } //Line 11 } //Line 12 Consider the accompanying definition of a recursive function. Which of the statements represent the general case?
Statements in Lines 7-11
Which of the following rules should you follow to solve the Tower of Hanoi problem?
The removed disk must be placed on one of the needles.
The ____ case is the case for which the solution is obtained directly.
base
A function is called ____ if it calls itself.
directly recursive
Tracing through ____ recursion is more tedious than tracing other recursive forms.
indirect
If every recursive call results in another recursive call, then the recursive function (algorithm) is said to have ____ recursion.
infinite
A recursive function in which the last statement executed is the recursive call is called a(n) ____ recursive function.
tail
How many needles are used in the Tower of Hanoi problem?
three