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
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
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.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
>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?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Sometimes the journey is more important than the destination?
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
You don't need to figure it out as it isn't your homework. :P
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
> I was thinking too much into the matter, and the correction was just the use of global declaration.
Why not pass the limit as another parameter?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
>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();
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>see its very simple you just need to use one for loop statement
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401