600 views
1 votes
1 votes
#include <stdio.h>
int K = 4;
int a[2];
unsigned int m;

int* check(unsigned int n) {
	int res = 1;
	int count = 0;
	for(int i=0;i<K;i++) 
		if(!(n&(1<<i))) {
			count++;
			res = 0;
		}
	a[0] = res;
	a[1] = count;
	return a;
}

int foo(unsigned int n,int i) {
	static int count;
	m = n;
	int *x = check(m);
	if(x[0]) return 0;
	count += x[1];
	foo(m|i,i+1);
	return count;
}
int main() {
	int x = foo(0,0);
	printf("%d\n",x);
}

value of x ___ ?

2 Answers

1 votes
1 votes

foo(0,0)
m=0
x=check(0)
res=1//initially  value of result is 1
count=0// initially value of count is 0

In iteration 1:

here count =1, res=0

In iteration 2:

count=2, res=0

Iteration 3:

count=3, res=0

Ieration 4:

count=4, res=0

So, a[0]=0,a[1]=4

Now, control returns back to foo() again and value of static count becomes 4

Now,

foo(m|i,i+1);

foo(0,1) called

--------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(0,1)

m=0

x=check(0)

check(0) will again return 4 0

it returns back to foo( ) and static count will return 4+4=8

Now, foo(1,2) will called

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(1,2)

m=1

x=check(1) is called

In check(1)

Now, the twist comes

here count will not increment for i=0

So, count value will be 3

and result 0

So, a will return 3 0

control comes back to foo()

and value of static count is 8+3=11

Now, foo(3,3) is called

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(3,3)

m=3

x=check(3)

In check(3)

count=2

res=0

So, a will return 2 0

control comes back to foo()

here static count value is 11+2=13

Now, again foo(3,4) is called

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(3,4)

m=3

x=check(3)

In check(3), count=2 and result=0

a will return 2 0

Now, in foo() static count value is 13+2=15

Now, foo(7,5) is called

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(7,5)

m=7

x=check(7)

In check(7) , count will be 1 and return 0

So, a will return 1 0

In foo , value of static count is 15+1=16

Next foo(7,6) is called

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(7,6)

m=7

check(7)

In check(7) a will return  1 0

So, when control returns too foo(), count will give 17

Next foo(7,7) will call

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(7,7)

m=7

Again check(7)  will return 1 0

Now, in foo static count value is 18

Next foo(7,8) will call

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(7,8)

m=7

At last check(7) is called

Again a will return 1 0

Value of static count is 19

foo(15,16) is called now

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

foo(15,16)

m=15

check(15)

Now it will return 0 1 and eventually terminate the function

So, Answer will be 19

0 votes
0 votes
#include <stdio.h>
int K = 4;
int a[2];
unsigned int m;

int* check(unsigned int n) {
	int res = 1;
	int count = 0;
	for(int i=0;i<K;i++) 
		if(!(n&(1<<i))) {//n bitwise ANDing with i
			count++;// count always become 4 
			res = 0;
		}
	a[0] = res;
	a[1] = count;
	return a;// a returning 4 0
}

int foo(unsigned int n,int i) {
	static int count;
	m = n;
	int *x = check(m); // At first returning 4 0
	if(x[0]) return 0; // As x[0]=1 , then it will not return 0
	count += x[1]; //count=0+4=4
	foo(m|i,i+1);// it will call foo(0,1), and foo() will call again
	return count;// after all call count will return 4+4+3+2+2+1+1+1+1=19
}
int main() {
	int x = foo(0,0);
	printf("%d\n",x);// it will print 19
}
edited by

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 2 days ago
39 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
99 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
251 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how