The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+3 votes
494 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)}$?

  1. $9$
  2. $8$
  3. $5$
  4. $2$
asked in Algorithms by Veteran (96.4k points)
edited by | 494 views

2 Answers

+7 votes
Best answer
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$
answered by Boss (7.4k points)
edited by
+3 votes

Final return value is 2 So option d

answered by Veteran (39.2k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

28,834 questions
36,688 answers
90,626 comments
34,641 users