recursion - how to calculate sum?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 3
Reputation: krizz is an unknown quantity at this point 
Solved Threads: 0
krizz krizz is offline Offline
Newbie Poster

recursion - how to calculate sum?

 
0
  #1
Oct 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: recursion - how to calculate sum?

 
0
  #2
Oct 10th, 2007
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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: kamlesh_ag07 is an unknown quantity at this point 
Solved Threads: 0
kamlesh_ag07 kamlesh_ag07 is offline Offline
Newbie Poster

Re: recursion - how to calculate sum?

 
0
  #3
Oct 10th, 2007
according to me the code can be writen in this way.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: recursion - how to calculate sum?

 
0
  #4
Oct 10th, 2007
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: recursion - how to calculate sum?

 
0
  #5
Oct 10th, 2007
>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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: recursion - how to calculate sum?

 
0
  #6
Oct 10th, 2007
Sometimes the journey is more important than the destination?

Val
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: recursion - how to calculate sum?

 
0
  #7
Oct 10th, 2007
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 :
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: recursion - how to calculate sum?

 
0
  #8
Oct 10th, 2007
You don't need to figure it out as it isn't your homework.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: krizz is an unknown quantity at this point 
Solved Threads: 0
krizz krizz is offline Offline
Newbie Poster

Re: recursion - how to calculate sum?

 
0
  #9
Oct 10th, 2007
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.

Originally Posted by iamthwee View 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?
Last edited by krizz; Oct 10th, 2007 at 5:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: recursion - how to calculate sum?

 
0
  #10
Oct 10th, 2007
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
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC