I need help with the following:

How do we calculate the sum of consecutive numbers from 1 to n and not from n to 1 by using recursion.

Eg. if n = 10

it should be 1+2+...+9+10 and not 10+9+...+2+1.

krizz
0
Newbie Poster

I need help with the following:

How do we calculate the sum of consecutive numbers from 1 to n and not from n to 1 by using recursion.

Eg. if n = 10

it should be 1+2+...+9+10 and not 10+9+...+2+1.

Jump to PostAssuming you understand how to write a recursive function, you need to establish what the base case is for this problem - when a parameter for the current term passed in is equal to the upper limit (10 in this case, but best also passed as a parameter for flexibility …

Jump to PostIt can be written that way, but it's not correct. It doesn't even compile.

Once you fix the two problems in your preprocessor directives, you'll find it is not a correct solution.

Once you've corrected the problem in main that causes you to not even see the answer …

Jump to Post>it should be 1+2+...+9+10 and not 10+9+...+2+1

But it gives you exactly the same answer, and all you are asking for is the end total right?

Jump to PostSometimes the journey is more important than the destination?

Val

Jump to PostThe quest here is not to get the result, but to get that scenario. If you just wanted to get the sum then this is suffice :

`#include<iostream> using namespace std; int Sum(int s) { int result; if( s == 1 ) return 1; else { result …`

vmanes
1,165
Posting Virtuoso

Assuming you understand how to write a recursive function, you need to establish what the base case is for this problem - when a parameter for the current term passed in is equal to the upper limit (10 in this case, but best also passed as a parameter for flexibility of use). Rather than the usual decreasing value passed in (the 10-1 version), your current term will be 1 in the first call, 2 in the next, and so on.

Val

kamlesh_ag07
0
Newbie Poster

according to me the code can be writen in this way.

```
#include <iostream>
#include <conio>
int sum(int);
int x;
void main()
{
int n ;
cin >> n ;
sum(n) ;
cout<<" the sum is"<<x ;
getch();
}
int sum(int a)
{
if(a)
{
return sum(a-1)+a;
}
}
```

vmanes
1,165
Posting Virtuoso

It can be written that way, but it's not correct. It doesn't even compile.

Once you fix the two problems in your preprocessor directives, you'll find it is not a correct solution.

Once you've corrected the problem in main that causes you to not even see the answer your sum( ) function returns, note that you have not solved the original problem. Your function works in the 10 9 8... direction.

Your `if( a )`

is clever approach, however it leaves your function with a warning of "not all control paths are followed."

You should test code before posting it to the world.

iamthwee

>it should be 1+2+...+9+10 and not 10+9+...+2+1

But it gives you exactly the same answer, and all you are asking for is the end total right?

vmanes
1,165
Posting Virtuoso

Sometimes the journey is more important than the destination?

Val

ChaseVoid
30
Junior Poster

The quest here is not to get the result, but to get that scenario. If you just wanted to get the sum then this is suffice :

```
#include<iostream>
using namespace std;
int Sum(int s)
{
int result;
if( s == 1 )
return 1;
else
{
result = Sum(s-1) + s;
return result;
}
}
int main()
{
int n = 0;
cout<<"Enter a limit : ";
cin>>n;
cout<<"The sum of numbers up to "<<n<<" is "<<Sum(n);
cin.ignore();
cin.get();
}
```

But we want to get the result such that it calculates from 1 till the limit.

```
for ( int i = 1; i < limit; i++ )
sum += i;
```

We want that scenario with recursion. I've trying to figure it out, but I can't :(

iamthwee

You don't need to figure it out as it isn't your homework. :P

krizz
0
Newbie Poster

The end total can be found easily by calculating from 10+9+...+2+1 but i am more interested in the scenario as one of my friends has already said..ie. I need it to process from 1 to n and not n to 1.

>it should be 1+2+...+9+10 and not 10+9+...+2+1

But it gives you exactly the same answer, and all you are asking for is the end total right?

vmanes
1,165
Posting Virtuoso

ChaseVoid's code gives a good implementation of the normal (1+2+3...) method of recursively solving this problem. Reread my first comments, and you should be able to modify that function to make it solve in the desired way.

Val

ChaseVoid
30
Junior Poster

> iamthwee : You don't need to figure it out as it isn't your homework.

Does it really matter whether it's my homework or not. We have a really interesting question here and trying to solve it is fun. You might know this answer, but I don't so I tried to solve it. And I'm really glad I did.

I was thinking too much into the matter, and the correction was just the use of global declaration.

```
#include<iostream>
using namespace std;
int n = 0;
int Sum(int s)
{
int result;
if( s == n )
return n;
else
{
result = Sum(s+1) + s;
return result;
}
}
int main()
{
cout<<"Enter a limit : ";
cin>>n;
cout<<"The sum of numbers up to "<<n<<" is "<<Sum(1);
cin.ignore();
cin.get();
}
```

this is what I got. But I'm still not so sure, when I simulated this program I found that first it increases the s from 1 to n and then it decreases from n -1 and also adds it to the result. While the program before, is the reverse. I haven't done much recursion, but still.. it seems weird to me. May I have some expert advice on this matter?

Salem
5,138
Posting Sage

Why not pass the limit as another parameter?

vmanes
1,165
Posting Virtuoso

First, your `int n = 0;`

is a global variable; not a recommended practice in most cases.

As I mentioned previously, your recursive function will need to know the current value it's to process (your parameter s) and the limit (your global var n). So simply add a parameter to the function that will be the upper limit value entered by the user.

What you see as the values going up (as desired) then down is the way that recursive functions work. It's kind of like a watch spring being wound up, then unwinding when released. (Sorry, bad analogy, nobody knows how a mechanical watch works any more!)

Why is the global variable not a good idea? It can be modified by any function in a program, and thus you have no real control over it. It can make debugging difficult, and it reduces the ability to reuse code in future projects. Use of global variables needs to be very well thought out and very well documented.

Val

ChaseVoid
30
Junior Poster

>Why not pass the limit as another parameter?

Well I could have done that, but then here `result = Sum(s+1) + s;`

I would have to pass two parameter as well. As I mentioned earlier, I don't know much in depth about recursion, so have no idea how or what to pass there. if you know, please tell me.

Narue
5,707
Bad Cop
Team Colleague

>I don't know much in depth about recursion, so have no idea how or what to pass there.

It doesn't really matter that the function is recursive. Just pass the parameter along without changing it and everything will be fine:

```
#include<iostream>
using namespace std;
int Sum(int s, int n)
{
int result;
if( s == n )
return n;
else
{
result = Sum(s+1, n) + s;
return result;
}
}
int main()
{
int n;
cout<<"Enter a limit : ";
cin>>n;
cout<<"The sum of numbers up to "<<n<<" is "<<Sum(1, n);
cin.ignore();
cin.get();
}
```

ChaseVoid
30
Junior Poster

Oh! i see now, since inside the Sum() function we are not manipulating variable n, it'll be. Thank you.

anirudhbsg
-2
Newbie Poster

I need help with the following:

How do we calculate the sum of consecutive numbers from 1 to n and not from n to 1 by using recursion.

Eg. if n = 10

it should be 1+2+...+9+10 and not 10+9+...+2+1.

see its very simple you just need to use one for loop statement:

for(int i=0;i<n;i++)

(sum=sum+i;

}

Narue
5,707
Bad Cop
Team Colleague

If it's so simple, why did you get it wrong? Read the post you quoted again. It says "by using recursion". A loop isn't recursion, it's iteration.

krizz
0
Newbie Poster

anirudhbsg
-2
Newbie Poster

If it's so simple, why did you get it wrong? Read the post you quoted again. It says "by using recursion". A loop isn't recursion, it's iteration.

yes i really apologize , i just missed that the discussion is on recursion and not iteration

i will definetely take care of this thing in future..

sorry again.....

Be a part of the DaniWeb community

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

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.