| | |
Recursive Function Problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have a problem with a program I'm writing. It's supposed to find the factorial of the number you input, but it's not. The problem seems to be that the recursive function isn't...well...recursing. Right now, when compiled, the program multiplies the number by the number before it. So if it's 5 that you put in, it gives you 20, instead of 120, which it would if the factorial thing worked.
Don't tell me the whole answer to the problem; it would be nice if you could just point me in the right direction.
Oh, and please remember that I'm a novice. I've only been coding lightly for about 3 or 4 months, perhaps.
Don't tell me the whole answer to the problem; it would be nice if you could just point me in the right direction.
Oh, and please remember that I'm a novice. I've only been coding lightly for about 3 or 4 months, perhaps.
And I guess it would help if I put the code in here...
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void recurse(int input) //the recurse function... { if (input == 0){ //if the input becomes 0, exit return; } recurse(input - 1); //calls itself } int main() { int input; cout<<"Input a number to find the factorial of:\n"; cin>>input; //get input value cin.get(); recurse (input); //calls recurse function cout<<input * (input-1)<<"\n"; //multiplies it all together cin.get(); }
Two problems:
1) need a return value to return the factorial. Your function does the recursion but doesn't calculate anything.
2) The function's return value should be int, not void.
1) need a return value to return the factorial. Your function does the recursion but doesn't calculate anything.
2) The function's return value should be int, not void.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
If a function returns, say, an int, you can use a call to that function in every place you could have used an int.
Examples:
So if you want to evaluate n!, your function should return that number for you to do whatever you like with it.
The purpose on making it recursive is that you know a base case (0! = 1 by definition) and you have
Combine the two things and remember to output the result.
Hope this helps.
Examples:
c++ Syntax (Toggle Plain Text)
#include <iostream> int sumFunction(int a, int b) { return a + b; } int main() { int a = 5, b = 7, c = 0; c = sumFunction(a, b); // c now is 5 + 7 = 12 b = a + sumFunction(a, c); // b now is 5 + (5 + 12) = 22; std:cout << "123 + 123 = " << sumFunction(123, 123) << std::endl; return EXIT_SUCCESS; }
So if you want to evaluate n!, your function should return that number for you to do whatever you like with it.
The purpose on making it recursive is that you know a base case (0! = 1 by definition) and you have
n! = (n-1)! * n (a recursive definition of the factorial).Combine the two things and remember to output the result.
c++ Syntax (Toggle Plain Text)
int factorial(int n) { if(n==0) { return 1; // 0! = 1 } return /* I leave it up to you what to return here. Hint: it should contain a call to factorial(int n) itself (hence recursion) */ }
Hope this helps.
•
•
Join Date: Dec 2008
Posts: 15
Reputation:
Solved Threads: 3
•
•
•
•
I think the problem is that I don't know how to take the output of a function and use it...
C++ Syntax (Toggle Plain Text)
int iReturn = recurse (input); or int iReturn = 0; iReturn = recurse (input);
Also, your factorial calculation should be done inside the recursive function.
Last edited by Liinker; Dec 23rd, 2008 at 6:24 pm.
I finally got this one! YES!!! Thanks everyone, I'm so ecstatic now. This thread is finally solved, I finally understand recursion, etc. etc. Hooray! And I was just beating myself up five minutes ago because I couldn't get it. Finally...yes...I feel so good about myself now. Thanks everyone for your help. Here's the completed program:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int factorial(int x); int main(){ int x; cout<<"Input a number to find the factorial of:\n"; cin >> x; cin.ignore(); cout<<"The factorial of " << x << " is " << factorial(x) << "\n"; cin.get(); } int factorial(int x){ if (x == 0){ return 1; } return x * factorial(x - 1); //it took me MONTHS to get THIS?!?! }
![]() |
Similar Threads
- recursive function problem (Java)
- Recursive Function to Reverse an Integer (C++)
- Recursive function not fully working (C++)
- need help with c++ recursive function (C++)
- Recursive function - checking for palindromes (C)
- need help reversing letters in a word (Java)
- Recursive prime number f(x) (C)
Other Threads in the C++ Forum
- Previous Thread: Flushing the input
- Next Thread: Getters
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






