search
Log In
14 votes
4.1k views

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(345, 10)}$?

  1. $345$
  2. $12$
  3. $5$
  4. $3$
in Algorithms
edited by
4.1k views
1
Sum of the digits in the given decimal number 345, 3+4+5 = 12 is the correct option.
0
would the answer have been any different for signed int ?

4 Answers

22 votes
 
Best answer

Red colour represents return values.

Answer is 12.


edited by
12 votes
A) The function returns the sum of digits of the given number.

so 5+4+3 = 12
2 votes

Following recursive calls

0 votes


∴ 5+4+3+0 = 12

0
What would be the reccurance equation for this question?
Answer:

Related questions

15 votes
3 answers
1
2.2k views
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)}$? $9$ $8$ $5$ $2$
asked Apr 21, 2016 in Algorithms jothee 2.2k views
14 votes
4 answers
2
4k views
Consider the code fragment written in C below : void f (int n) { if (n <= 1) { printf ("%d", n); } else { f (n/2); printf ("%d", n%2); } } Which of the following implementations will produce the same output for $f(173)$ as the above code? P1 P2 void f (int n) { if (n/2) { f(n/2 ... ("%d", n); } else { printf ("%d", n%2); f (n/2); } } Both $P1$ and $P2$ $P2$ only $P1$ only Neither $P1$ nor $P2$
asked Oct 29, 2014 in Algorithms Ishrat Jahan 4k views
10 votes
5 answers
3
2.3k views
Consider the code fragment written in C below : void f (int n) { if (n <=1) { printf ("%d", n); } else { f (n/2); printf ("%d", n%2); } } What does f(173) print? $010110101$ $010101101$ $10110101$ $10101101$
asked Oct 29, 2014 in Algorithms Ishrat Jahan 2.3k views
21 votes
5 answers
4
4.5k views
What is the value printed by the following C program? #include<stdio.h> int f(int *a, int n) { if (n <= 0) return 0; else if (*a % 2 == 0) return *a+f(a+1, n-1); else return *a - f(a+1, n-1); } int main() { int a[] = (12, 7, 13, 4, 11, 6); printf("%d", f(a, 6)); return 0; } $-9$ $5$ $15$ $19$
asked Sep 30, 2014 in Algorithms jothee 4.5k views
...