| | |
recursion - how to calculate sum?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
according to me the code can be writen in this way.
C++ Syntax (Toggle Plain Text)
#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; } }
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
You should test code before posting it to the world.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Sometimes the journey is more important than the destination?
Val
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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 :
But we want to get the result such that it calculates from 1 till the limit.
We want that scenario with recursion. I've trying to figure it out, but I can't
C++ Syntax (Toggle Plain Text)
#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(); }
C++ Syntax (Toggle Plain Text)
for ( int i = 1; i < limit; i++ ) sum += i;
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
Last edited by krizz; Oct 10th, 2007 at 5:49 pm.
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
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- C++ Recursion: Sum Array (IT Professionals' Lounge)
- Calculating Sum of large unsigned integers. (C++)
- what is C++ builder code for sum (C++)
- C++ help with student array problem (C++)
- Pleae help!!! Homework woes! (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Help me with "Math Library" Related coding =) (C++)
- If Else Loops (Java)
- Using a for loop to sum an integer n and call function add_it (C)
- Math tutorial (C++)
Other Threads in the C++ Forum
- Previous Thread: cin.getline not working in for loop or...?
- Next Thread: ambiguous symbol?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






