Question

Consider this function declaration: void quiz(int i) { if (i > 1) { quiz(i / 2);...

Consider this function declaration: void quiz(int i) { if (i > 1) { quiz(i / 2); quiz(i / 2); } cout << "*"; } How many asterisks are printed by the function call quiz(5)?

Answer: The first time, i = 5 which is greater than 1, so the function is called twice with a value of (5/2) = 2. Since 2 > i, the function is called 4 times with a value of 1, and is not called anymore, since i <= 1.

This gives a total of 1 + 2 + 4 function calls, with one asterisk being printed for each call. Therefore, 7 asterisks are printed.

now my question is "Since 2 > i, the function is called 4 times with a value of 1" why quiz(2) is called 4 times? please explain me this. I am having confusion. I understand the when i is 5 quiz function is called 2 times because there are two recursive calls "quiz(i / 2); quiz(i / 2);"

Homework Answers

Answer #1

It is because of the fact when you call the function for the first time * will be printed for one time and two recursive calls will be called with the value of 2 and for each recursive call with the value of 2 recursive function will be called two times with the value of 1 and two asterisk will be printed so when you call function with the value of 5 only 1 asterisk will be printed and for the value of 2 two instances will be called with the value of 2 and for the instances withthe value of 2 , the recursive function will be called for two times each so for the value of 2 , for the value of 2 , four instances will be called because we are calling two instances of recursive function in the quiz function and it recursive call will be keep on increasing in the multiple of two.

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
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?
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...
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
2. Consider the following program written in C syntax: void swap(int a, int b) {   ...
2. Consider the following program written in C syntax: void swap(int a, int b) {    int temp;    temp = a;    a = b;    b = temp; } void main() {    int value = 2, list[5] = {1, 3, 5, 7, 9};    swap(value, list[0]);    swap(list[0], list[1]);    swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three...
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),...
C++ visual studios this function has to run twice. and the "count" variable needs to be...
C++ visual studios this function has to run twice. and the "count" variable needs to be "2" on the second run. How can i do that? The parameters can not change as well void printArray(const int *array, int n) {    int count = 1;    char check;    if (check == 'k')    {        count++;    }    cout << "Value " << count << " array contents." << endl;    cout << "------------------------" << endl;   ...
What is wrong with the following function: public static void funMethod() { int i=1; while (i...
What is wrong with the following function: public static void funMethod() { int i=1; while (i < 10) { System.out.println("i="+i); i++; } } Select one: a. nothing is wrong with this code. b. loop control variable not initialized c. loop control variable is never updated d. loop control variable not declared e. loop control variable never resolves to true
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
Write a C function to compute the following output (int) based on two input (int) variables:...
Write a C function to compute the following output (int) based on two input (int) variables: output = a*a + b*b - 2*a*b If the value of output is '0', then return -1 instead. Call the C function 4 times from main() with values of 'a' and 'b' as follows and print output values for each case. a = 3, b = 4 a = 1, b = 1 a = -2 b = 3 a = -4 b =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT