Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the same recursive C function that t... Start Learning for Free
Consider the same 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 foo when it is called as foo(513, 2)?
  • a)
    9
  • b)
    8
  • c)
    5
  • d)
    2
Correct answer is option 'D'. Can you explain this answer?
Verified Answer
Consider the same recursive C function that takes two argumentsunsigne...
foo(513, 2) will return 1 + foo(256, 2). All subsequent recursive calls (including foo(256, 2)) will return 0 + foo(n/2, 2) except the last call foo(1, 2) . The last call foo(1, 2) returns 1. So, the value returned by foo(513, 2) is 1 + 0 + 0…. + 0 + 1. The function foo(n, 2) basically returns sum of bits (or count of set bits) in the number n.
View all questions of this test
Most Upvoted Answer
Consider the same recursive C function that takes two argumentsunsigne...
The given recursive C function seems to be incomplete, as the condition in the if statement is missing. However, based on the provided code, I can make some assumptions and provide a possible implementation.

Assuming that the missing condition in the if statement is "n > 0", the function could be implemented as follows:

```c
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0) {
return foo(n - 1, r) + 2 * r;
}
return 0;
}
```

This function takes two unsigned int arguments, `n` and `r`, and recursively calculates the sum of `r` added to itself `n` times.

Here's a step-by-step explanation of how the function works:

1. The function checks if `n` is greater than 0. If it's true, the function proceeds with the recursive call; otherwise, it returns 0.
2. In the recursive call, the function decrements `n` by 1 and adds `2 * r` to the result of the recursive call with the updated `n`.
3. The recursion continues until `n` reaches 0, at which point the function returns 0.
4. As the recursive calls unwind, the function adds `2 * r` to the result of each recursive call, effectively summing `r` `n` times.

Here's an example usage of the `foo` function:

```c
unsigned int result = foo(3, 5);
printf("%u\n", result); // Output: 30
```

In this example, the function `foo(3, 5)` is called. It calculates the sum of 5 added to itself 3 times, resulting in a sum of 30.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer?
Question Description
Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer?.
Solutions for Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer?, a detailed solution for Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? has been provided alongside types of Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the same recursive C function that takes two argumentsunsigned 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 foo when it is called as foo(513, 2)?a)9b)8c)5d)2Correct answer is option 'D'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev