Question

QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...

QUESTION 1

For the following recursive function, find f(5):

int f(int n)
{
if (n == 0)
   return 0;
else
   return n * f(n - 1);
}

A.

120

B.

60

C.

1

D.

0

10 points   

QUESTION 2

Which of the following statements could describe the general (recursive) case of a recursive algorithm?

In the following recursive function, which line(s) represent the general (recursive) case?

void PrintIt(int n ) // line 1

{ // line 2

if (n > 0) //line 3

{ //line4

cout << "Again" << endl; //line 5

PrintIt(n - 1); // line 6

} // line 7

else // line 8

cout << n << endl; // line 9

} // line 10

A.

Lines 5 6

B.

Lines 5 9

C.

Lines 6 9

D.

Line

E.

None of the above.

10 points   

QUESTION 3

Recursion is memory-intensive because:

A.

Recursive functions tend to declare many local variables.

B.

Many copies of the function code are created

C.

Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack.

D.

It requires large data values

E.

None of these answers are correct.

10 points   

QUESTION 4

Recursion uses more memory space than iteration because

A.

it uses stack instead of queue.

B.

every recursive call has to be stored.

C.

both A & B are true.

D.

None of the above are true.

10 points   

QUESTION 5

Read the following code and determine what goes in blank #1.

int Length(NodeType* list)

// Pre: list has been initialized.

// Post: Length returns the number of elements in list. No counter is kept as

// part of the list. This function is recursive.

{

if (______________) // 1

_______________; // 2

else _______ // 3

}

A.

list == NULL

B.

list->info = 0

C.

list->next == NULL

D.

list != NULL

E.

None of these answers is correct.

Homework Answers

Answer #1

Answers:

(1). You can trace the output by performing the execution as follows:

f(5) = 5 * f(5-1) // because n=5 is not = 0 so it goes in else part

So, f(5) = 5 * f(4)
= 5 * 4 * f(3) // because n=4 is non-zero so again in else part
= 5 * 4 * 3 * f(2)
= 5 * 4 * 3 * 2 * f(1)
= 5 * 4 * 3 * 2 * 1 * f(0)
= 5 * 4 * 3 * 2 * 1 * 0 // for n=0, f() returns 0
= 0

So, the answer is (D) 0.

(2). The recursive case / general case can easily be fond by looking in the function code. Recursion means the function is calling itself. So, search for the name of function PrintIt() in its code, it is at line 6. So, this is the answer, as it is not an option, you can consider line 5 and combine it because it only prints that now we are going to do recursion. So, the answer is (A) Lines 5 6.

(3). The correct option is (C).

The reason behind this is, when a function call is made, the return address is stored first in the stack, then the control is transferred to the called function, then after finishing the execution, again control goes to the calling function. So, for every function call in the recursion, it needs to store in the stack. So, correct option is Option (C) because it needs more memory and it is memory intensive.

(4). The answer is (B).

There is no concept of using queue for iteration. So, the option (a) is not possible. As I wrote in the question (3), every recursive call need to be stored in stack along with the return address to execute the sentence perfectly. So, option (B) is correct.

(5). The answer is (A) list == NULL.

This is also a recursive function and in the recursive function, see the question (1). In the if condition, we are writing the condition when the recursion is needed to stop the execution. The recursive part comes in the else statement.

The given function is to find the length of the list. So, in the if part we check whether the node is NULL then we need to stop the execution otherwise we go one level down and find the total number of levels for that node in the else part. So, the if part checks whether the list pointer is NULL. So, the answer (A) is correct for this question.

Please comment if there is any query. Thank you. :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: {...
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: { 03: list<int> L; 04: for (int i = 0; i < N; ++i) 05: { 06: copy (source, source + i, front_inserter<int>(L)); 07: } 08: for (int j: L) 09: { 10: if (find(dest.begin(), dest.end(), j) != dest.end()) 11: dest.push_back(j); 12: } 13: } and the following list of complexity values: A: O(1), B: O(log N), C: O(N), D: O(N log N), E: O(N2),...
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
Given the following function in C++: int main(void) { int a = 2; int b =...
Given the following function in C++: int main(void) { int a = 2; int b = myFunction(a); a = b + 1; b = myFunction(a); cout << ”b = ” << b << endl; return 0; } int z = myFunction(int x) { static int n = 0; n = n + 1; int z = x + n; return z; } What is printed by the cout on the screen?
Please find the errors of the following code a) int x[5]; int k = 10; for...
Please find the errors of the following code a) int x[5]; int k = 10; for (k = 0; k <= 5; k++) { x[k] = k -1; } b) int x[5]; for (k = 1; k <= 5; k++) { cout << x[k-1] << endl; } c) int x[5]={1,2,3,4,5}, y[5]; y = x; for (k = 0; k < 5; k++) { cout << y[k] << endl; } d) int size; cin >> size; int myArr[size];
For different input n, i.e., n = 1, n = 10, n = 20, write down...
For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation. #include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;     float f=2.22;     float h=-7.5;     char a='S';     char b='M';     cout<<i<<"\t"<<j<<"\t"<<k<<endl;     Cout<<f<<"\t"<<h<<endl;     cout<<a<<"\t"<<b<<endl;     return 0;     }
C program. Given the following code: int add_and_double(int a, int b) { int result = (a...
C program. Given the following code: int add_and_double(int a, int b) { int result = (a + b) * 2; return result; } int main() {   int val; val = add_and_double(12, 40); return 0; } where the function main() (i.e. the caller) calls the function add_and_double() (i.e. the callee). Which of the following actions are taken when main() calls the function add_and_double()?  Select all that apply. a. Once the callee returns, the caller frees the stack memory for the callee's function...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.