943,989 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11474
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 10th, 2007
0

recursion - how to calculate sum?

Expand Post »
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.
Last edited by krizz; Oct 10th, 2007 at 1:06 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krizz is offline Offline
3 posts
since Oct 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. #include <conio>
  4.  
  5. int sum(int);
  6.  
  7. int x;
  8.  
  9. void main()
  10. {
  11. int n ;
  12.  
  13. cin >> n ;
  14.  
  15. sum(n) ;
  16.  
  17. cout<<" the sum is"<<x ;
  18.  
  19. getch();
  20.  
  21. }
  22.  
  23. int sum(int a)
  24. {
  25. if(a)
  26. {
  27. return sum(a-1)+a;
  28.  
  29. }
  30. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kamlesh_ag07 is offline Offline
6 posts
since Oct 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

>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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

Sometimes the journey is more important than the destination?

Val
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

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 :
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int Sum(int s)
  5. {
  6. int result;
  7. if( s == 1 )
  8. return 1;
  9. else
  10. {
  11. result = Sum(s-1) + s;
  12. return result;
  13. }
  14.  
  15. }
  16.  
  17. int main()
  18. {
  19. int n = 0;
  20. cout<<"Enter a limit : ";
  21. cin>>n;
  22.  
  23. cout<<"The sum of numbers up to "<<n<<" is "<<Sum(n);
  24.  
  25. cin.ignore();
  26. cin.get();
  27. }
But we want to get the result such that it calculates from 1 till the limit.
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 1; i < limit; i++ )
  2. sum += i;
We want that scenario with recursion. I've trying to figure it out, but I can't
Reputation Points: 40
Solved Threads: 12
Junior Poster
ChaseVoid is offline Offline
116 posts
since Aug 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

You don't need to figure it out as it isn't your homework.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

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.

Click to Expand / Collapse  Quote originally posted by 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?
Last edited by krizz; Oct 10th, 2007 at 5:49 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krizz is offline Offline
3 posts
since Oct 2007
Oct 10th, 2007
0

Re: recursion - how to calculate sum?

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: cin.getline not working in for loop or...?
Next Thread in C++ Forum Timeline: ambiguous symbol?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC