edited by
5,465 views
18 votes
18 votes

Consider the following recursive C function that takes two arguments.

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

What is the return value of the function $\text{foo}$ when it is called as $\text{foo(513, 2)}$?

  1. $9$
  2. $8$
  3. $5$
  4. $2$
edited by

2 Answers

Best answer
28 votes
28 votes
The function returns the sum of digits in a binary representation of the given number

so $1+0+0+0+0+0+0+0+0+1 = 2$

Correct Answer: $D$
edited by
Answer:

Related questions

18 votes
18 votes
3 answers
1
26 votes
26 votes
4 answers
2
29 votes
29 votes
6 answers
3
Kathleen asked Sep 25, 2014
13,707 views
What value would the following function return for the input $x=95$?Function fun (x:integer):integer; Begin If x 100 then fun = x – 10 Else fun = fun(fun (x+11)) End;$...