



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 1704 Homework 6 Recursion Spring 2000
Instructions: Opscan forms will be passed out in class and collected at class on Thursday, Nov 14. No late opscans will be accepted.
For questions 3 and 4, consider the following recursive function:
void PrintIt(int n) {
if (n != 0) { // Line 1 cout << "Again" << endl; // 2 PrintIt(n - 1); // 3 else { cout << n << endl; // 4 return; // 5 } }
Yes, if x > 0
Yes, if x < 0
Yes, x >= 0
Yes, x <= 0
No
int Func( int n ) { if (n == 5) return 5; else return ( 2 * Func(n + 1) ); }
int Sum( int Low, int Limit ) {
if ( Low > Limit ) return 0; if ( ) return Limit; else return ( Low + Sum(Low + 1, Limit) }; }
What should the missing condition in the if statement be?
Low == 1
Limit == 1
Low == Limit
Low > Limit
Low < Limit
None of these
void PrintArr(const int array[], int first, int last) {
if (first > last) cout << "Done"; else { PrintArr(array, first + 1, last); cout << array[first]; } }
which code segment below produces the same output as the function call: PrintArr(arr, 0, 5)?
for (int i = 0; i < 6; i++) cout << array[i]; cout << "Done";
cout << "Done"; for (int i = 0; i < 6; i++) cout << array[i];
for (int i = 0; i < 6; i++) { cout << array[i]; cout << "Done"; }
for (int i = 5; i >= 0; i--) cout << array[i]; cout << "Done";
cout << "Done"; for (int i = 5; i >= 0; i--) cout << array[i];
None of these
int Sum( int n ) { if ( n < 8 ) return ( n + Sum(n) ); else return 2; }
false
true
Size++
Size - 1
Value
None of these
For questions 16 through 20, consider the following recursive function, which is intended to print the data elements from a SList object (with member functions as specified in the notes) in reverse order. Note: we assume that there is an operator<< for the type Item.
void RevPrint(SList& LL, ostream& Out) {
if ( ) return; // Line 1: terminate recursion
LL.goToTail(); // Line 2 Item toPrint; // Line 3 ; // Line 4: obtain data to print Out << toPrint << endl; // Line 5
; // Line 6: recursive case }
!LL.isEmpty()
LL.isEmpty()
LL.Head != NULL
Nothing goes there
None of these
toPrint = LL.Get()
LL.Advance()
LL.Delete(toPrint)
Nothing goes there
None of these
return
return RevPrint(LL, Out)
LL.RevPrint()
RevPrint(LL, Out)
None of these
Yes, it reverses the list.
Yes, it leaves the list empty.
Yes, it disconnects the list.
No.
None of these
For questions 21 and 22, consider the following recursive function:
int findFactorPower(int D, int N) {
if ( (D <= 0) || (N < 0) ) return 0; // catch invalid parameters
int Times; // # of times D goes into N
if ( N % D == 0) // Does D go into N? Times = 1 + findFactorPower(D, N/D); // Count it and see if it // goes in again. else Times = 0; // Nope, don't check further.
return Times; }
For questions 23 through 25, consider the Knapsack Problem stated in the course notes, and the recursive function given there for solving the problem. If there is a solution, it is a set of values; the function discovers the solution by performing a sequence of recursive calls until the goal sum is achieved, or is found to be impossible. Although the given function does not build a representation of the solution set, it could be easily modified to do so. In any case, the function does build the solution in a virtual sense, and each time the function is called it determines whether the set it is currently considering is a possible solution. Keeping that in mind… suppose that the function is called with an initial goal of 10, and the following array of "candidate" values:
int ray[5] = {3, 5, 7, 11, 13};