0
#include <iostream>

using namespace std;

int findSumofOdds(int n);

int main()
{

  int n = 88;
  int x;

  x = findSumofOdds(n);
  cout << x << endl;


  return 0;
}

int findSumofOdds(int n)
{
  if (n != 1)
  {
    if( n % 2 == 0)
      n = (n - 1);

    return(findSumofOdds(n-1) + 1);
  }
  else
    return 1;
}

Why isn't this recursion working? It tries to run and then crashes. Please let me know. My teacher said that it would work but doesn't.

1
  • You know that there's a closed-form formula for that, don't you? 1 + 3 + 5 + ... + 2n-1 = n*n. Commented Apr 23, 2013 at 14:53

3 Answers 3

5

When n is even, you are decrementing n by two. If it skips over n == 1, it will recurse until it causes a stack overflow. Since n starts out at 88, that's what's happening.

int findSumofOdds(int n)
{
    if (n != 1)
    {
        if( n % 2 == 0)
            n = (n - 1); // <== first decrement

        return(findSumofOdds(n-1) + 1); // <== second decrement
    }
    else
        return 1;
}

Also, you seem to be counting the number of odd numbers, not adding them. My guess is that you actually want something like:

int findSumofOdds(int n)
{
    if (n != 1)
    {
        if( n % 2 == 0)
            return(findSumofOdds(n - 1));

        return(findSumofOdds(n-1) + n); // or + 1 to just count
    }
    else
        return 1;
}

If you want to practice recursion, that's fine. But there's a much simpler way to write a function to sum the odd numbers up to and including n:

int fundSumofOdds(int n) {
    n = (n + 1) / 2;
    return n * n;
}

This is because there's a general formula:

1 + 3 + 5 + ... + 2n-1 = n2

Sign up to request clarification or add additional context in comments.

Comments

0

You have to make it

if (n > 1)

Consider n = 2 here

 if (n != 1)
 {
   if( n % 2 == 0)  // Yes
      n = (n - 1);  // n = 1

   return(findSumofOdds(n-1) + 1);  // n = 0  <-- will not stop.

and change this too. right now it is just counting the number of odd numbers. You need to sum them.

   return(n + findSumofOdds(n - 1));
}
else
    return 0;

Comments

0

If you print n in findSumofOdds you can see what is happening -- n becomes negative and you get infinite recursion. If your program doesn't crash earlier, you can get integer overflow (when n goes below the minimum value for int) which yields undefined behavior. To correct this you can do this:

int findSumofOdds(int n)
{
    if(n < 1)
    {
        return 0;
    }

    if(n % 2 == 0)
    {
        return findSumofOdds(n - 1) + 1;
    }
    return findSumofOdds(n - 2) + 1;
}

You can subtract 2 from n in the last statement, because you only need odd numbers and you know that n can't be even at that point (because of if(n % 2 == 0)).

Also, do you need to find the sum of all odd numbers smaller than n (e.g. 4 (== 1 + 3) for n=5) or do you just need to count them (which is what you are doing now)? If you want to sum the numbers, you have do add n instead of 1 when returning.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.