What will be the output of my recursive function in C -


consider following recursive c function takes 2 arguments.

unsigned int foo(unsigned int n, unsigned int r) {       if (n > 0)          return (n % r) + foo(n / r, r);       else           return 0;  } 

what value of function foo when called foo(512,2)?

this code ,actually recursion.

follow return happened:

if n == 0; return 0;  if n == 1; return 1+foo(0,2)  if n == 2; return 0 + foo(1,2);  if n == 4; return 0 + foo(2,2);    ...  if n == 2^n  return 0 + foo(0+foo(z^n-1,2));   ....   foo(512,2) == foo (2^n,2) == 0+f(1,2) == 1 +f(0,2) = 1; 

it return 1.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -