retagged by
20,281 views
40 40 votes

Consider the following $\text{C}$ program:

#include<stdio.h>

int counter=0;

int calc (int a, int b) {
        int c;
        counter++;
        if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }
}

int main() {
        calc(4, 81);
        printf("%d", counter);
}

The output of this program is ______.

7 Answers

Best answer
60 60 votes
int main() {
        calc(4, 81);
        printf("%d", counter);
}
printf("%d", counter);
So we need only counter value.
Each function increments counter value by 1. Goal is to find the number of function calls.
Squence of function calls:
calc(4, 81) ---> calc(4, 27) ---> calc(4, 9) ---> calc(4, 3) ---> return

4 function calls.
counter = 4
edited by
8 8 votes

There is no need to calculate return value we just need value of counter.

So answer is 4.

3 3 votes
#include<stdio.h>

int counter=0;

int calc (int a, int b) {
        int c;
        counter++;
        if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }
}

int main() {
        calc(4, 81);
        printf("%d", counter);
}

Here counter is a global variable, you can use anywhere of the program, 

 Now when main() call the calc() by the value (4,81) 

--> counter=counter+1 so now counter=1.

Now for 81 not eq to 3 so else block got executed and Now b=81/3=27.

Again counter increased by 1 and now counter =2.

In this way when b=3 counter will become 4 and cacl() return 64;

counter=3

int calc(4,3)

{

int c;

counter++;

if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }

}

So, ans is 4.

2 2 votes
The main function is just printing counter value. There's no need to calculate the values of return statements in the function calc().

 

Therefore , Answer is 4.
1 1 vote

we just need to count the no,, of function calls have been made to determine the value of counter

i.e calc(4,81) - function call no 1

     calc(4,27) - function call no 2

     calc(4,9) - function call no 3

     calc(4,3) - function call no 4

since each function call increments the value of counter variable by 1 and it is a global varible which is stored in static memory section it will retain it's value.

Therefore, the value of counter =4

Answer:
Position:
Show:

Related questions

55 55 votes
10 answers 10 answers
23.4k
23.4k views
gatecse asked Feb 14, 2018
23,365 views
Consider the following program written in pseudo-code. Assume that $x$ and $y$ are integers.Count (x, y) { if (y !=1 ) { if (x !=1) { print("*"); Count (x/2, y); } else {...
54 54 votes
7 answers 7 answers
28.5k
28.5k views
gatecse asked Feb 14, 2018
28,493 views
Consider the following C code. Assume that unsigned long int type length is $64$ bits.unsigned long int fun(unsigned long int n) { unsigned long int i, j=0, sum = 0; for(...
76 76 votes
7 answers 7 answers
25.6k
25.6k views
gatecse asked Feb 14, 2018
25,586 views
#include<stdio.h void fun1(char* s1, char* s2){ char* temp; temp = s1; s1 = s2; s2 = temp; } void fun2(char s1, char s2){ char* temp; temp = *s1; *s1 = *s2; *s2 = temp;...
87 87 votes
11 answers 11 answers
36.8k
36.8k views
gatecse asked Feb 14, 2018
36,849 views
Consider the following C program:#include<stdio.h struct Ournode{ char x, y, z; }; int main() { struct Ournode p={'1', '0', 'a'+2}; struct Ournode *q=&p; printf("%c, %c",...