-3

Trying to figure out where I am going wrong in this code, I realize I keep getting 1 because that's what am I passing in the function but how else can I do this?

#include <stdio.h>
#include <stdlib.h>
int totalOdd();

int main(){
    printf("%d\n",totalOdd(1));
}

int totalOdd(int n){
int odd = n;
    if(odd >= 100){
    return 0;
}
    else{
    totalOdd(odd+2);
}
return odd;
}
4
  • 8
    You should do something with the return value of the recursive call...
    – Eugene Sh.
    Commented Mar 22, 2017 at 22:06
  • 4
    You never do any addition...
    – Brick
    Commented Mar 22, 2017 at 22:06
  • You're code would be more general if you structured it to count down. That is, start with totalOdd(100) or perhaps completely change stream with totalOdd(1, 100) Commented Mar 22, 2017 at 22:11
  • 1
    Please learn how to indent your code correctly. By that you will also learn to better understand the structure of your program. Commented Mar 22, 2017 at 22:18

3 Answers 3

2

try this one one :

#include <stdio.h>
#include <stdlib.h>
int totalOdd(int);

int main(){
    printf("%d\n",totalOdd(1));
  }

   int totalOdd(int n)
{
   int odd = n;
      if(odd > 100){
          return 0;
         }
       else{
          return (n+totalOdd(odd+2));
         }
}

in your code , addition was missing

0
#include <stdio.h>
#include <stdlib.h>

int totalOdd();

int main(){
    printf("%d\n",totalOdd(1));
}

int totalOdd(int odd){
    if(odd >= 100)
    return 0;

return (odd + totalOdd(odd + 2));

}
2
  • This code doesnt seem to work the opposite, for when I want to add even numbers from 1 - 100, I get 2450 instead of 2550. Commented Mar 22, 2017 at 22:29
  • Change >= to >
    – Barmar
    Commented Mar 22, 2017 at 22:31
0

Not a complete answer, because this sounds like homework, but here’s an example of how to write a very similar function, first recursively, and then a more efficient tail-recursive solution.

#include <stdio.h>
#include <stdlib.h>

unsigned long factorial1(const unsigned long n)
{
/* The naive implementation. */
  if ( n <= 1U )
    return 1; // 0! is the nullary product, 1.
  else
    return n*factorial1(n-1);
 /* Notice that there is one more operation after the call to
  * factorial1() above: a multiplication.  Most compilers need to keep
  * all the intermediate results on the stack and do all the multiplic-
  * ations after factorial1(1) returns.
  */
}

static unsigned long factorial_helper( const unsigned long n,
                                       const unsigned long accumulator )
{
/* Most compilers should be able to optimize this tail-recursive version
 * into faster code.
 */
  if ( n <= 1U )
    return accumulator;
  else
    return factorial_helper( n-1, n*accumulator );
 /* Notice that the return value is simply another call to the same function.
  * This pattern is called tail-recursion, and is as efficient as iterative
  * code (like a for loop).
  */
}

unsigned long factorial2(const unsigned long n)
{
  return factorial_helper( n, 1U );
}

int main(void)
{
  printf( "%lu = %lu\n", factorial1(10), factorial2(10) );
  return EXIT_SUCCESS;
}

Examining the output of both gcc -O -S and clang -O -S on the above code, I see that in practice, clang 3.8.1 can compile both versions to the same optimized loop, and gcc 6.2.0 does not optimize for tail recursion on either, but there are compilers where it would make a difference.

For future reference, you wouldn’t solve this specific problem this way in the real world, but you will use this pattern for other things, especially in functional programming. There is a closed-form solution to the sum of odd numbers in a range. You can use that to get the answer in constant time. You want to look for those whenever possible! Hint: it is the sum, from i = 0 to 100, of 2 i + 1. Do you remember a closed-form formula for the sum of i from 0 to N? 0, 1, 3, 6, 10, 15, ...? The proof is often taught as an example of a proof by induction. And what happens to a sum from 0 to N when you multiply and add by constants?

As for my example, when I have had to compute a factorial function in a real program, it was for the purpose of computing a probability distribution (specifically, the Poisson distribution) for a simulation, and I needed to calculate the factorial of the same numbers repeatedly. Therefore, what I did was store a list of all the factorials I’d already calculated, and look up any number I saw again in that list. That pattern is called memoization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.