I'm trying to create a program that will sum all of the numbers from 1 to n, which the user inputs. I have to use recursion to calculate the sum. For example if the user inputs 5 it will return the sum of 1,2,3,4,5.

Here is what I have so far:

#include <iostream>
using namespace std;

int sum(int);

int main()
{
        int number;

        cout << "Enter an integer: ";
        (cin >> number).get();

        cout << "The sum of the numbers up to " << number << " is " << sum << endl;

        cin.get();
	return 0;
}

int sum(int n)
{
        if (n == 1)
            return 1;
        else
            return n + sum(n + 1);
}

I'm having problems with the recursion calculation. It is giving me extraordinarily large answers. Can anyone help?

Recommended Answers

All 7 Replies

for this kind of problem, you should pass the limit number to the function. Your recursive function calls itself with an a argument smaller that what it received, until the argument is eventually 1.

What's (cin >> number).get(); this doing?

(cin >> number).get(); is just how my instructor taught us to retrieve and store data. Is it wrong?

I can't quite grasp what you're saying. I'm still new to recursive functions.

int sum(int n)
{
        if (n == 1)
            return 1;
        else
            return n * (n+1) / 2;
}

I decided to do little reading, and thought I had it. The base case is first and is a way to solve the problem without recursion. All other circumstances are handled using recursion. So if the user inputs 1, the sum is obviously 1. All other circumstances are handled by the formula n * (n+1) / 2. However, i'm getting the exact same result as I was before which is baffling. It seems that no matter how I alter the function or what I input the result is always 4,200,984.

Well, your input method works, it's just odd. I suppose his intent is that it will get the input number and then remove the newline from the input stream(?).

As to the recursive function, check your text or notes. You have a correct base case, but you must ensure that every time the function calls itself, it's doing so with a value that gets closer to the base case. Your function calls it with a value larger than the input, so you're adding up lots of big numbers and getting further from it ever ending. Eventually the stack overflows (you're out of memory) and the program crashes.

That is, if you actually called the function. You ask your program to display "sum" - which is the function. So what get's displayed is the address of the function. You need to call the function, passing it the argument "numbers".

#include <iostream>
using namespace std;

int sum(int);

int main()
{
        int number;

        cout << "Enter an integer: ";
        (cin >> number).get();

        cout << "The sum of the numbers up to " << number << " is " << sum(number) << endl;

        cin.get();
	return 0;
}

int sum(int n)
{
        if (n == 1)
            return 1;
        else
            return n * (n+1) / 2;
}

I finally got it to work, and you were absolutely right. I simply forgot to include sum(number) . Thank you so much!

On your other point, if (cin >> ****).get(); isn't traditional what exactly should I be using? Simple cin statements?

Where did return n * (n+1) / 2; come from?

You no longer have a recursive function, you're simple applying the formula for summing 1 to n. (I forget which budding mathematician baffled his teacher with this one way back when.)

Usually we just do the cin >> number; If your problem requires removing the newline from input stream, do that in a separate statement, such as cin.ignore( 10, '\n' ); where you can put any number of characters to be ignored you think necessary.

Point 1:
n*(n+1)/2 is even valid for n=1, so you don't need a degenerate case of n=1.
Point 2:
as vmanes said, your base case is good, but look at your recursive case: return (n+sum(n+1)) Try to think what would happen if I call sum(2)?
it would return 2+sum(2+1) which inturns means 2+3+sum(3+1) which inturns means 2+3+4+sum(4+1)
So, sum(2)=2+sum(3)=2+3+sum(4)=2+3+4+sum(5) and so on.

Ask yourself: where will it end? to infinity!
So, you should know that there is some blunder in return (n+sum(n+1))
Correct it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.