943,608 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1166
  • C++ RSS
Dec 22nd, 2008
0

Recursive Function Problem

Expand Post »
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.
Similar Threads
Reputation Points: 17
Solved Threads: 5
Junior Poster
orwell84 is offline Offline
100 posts
since Nov 2008
Dec 22nd, 2008
0

Re: Recursive Function Problem

And I guess it would help if I put the code in here...

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. void recurse(int input) //the recurse function...
  4. {
  5. if (input == 0){ //if the input becomes 0, exit
  6. return;
  7. }
  8. recurse(input - 1); //calls itself
  9. }
  10. int main()
  11. {
  12. int input;
  13.  
  14. cout<<"Input a number to find the factorial of:\n";
  15. cin>>input; //get input value
  16. cin.get();
  17.  
  18. recurse (input); //calls recurse function
  19. cout<<input * (input-1)<<"\n"; //multiplies it all together
  20. cin.get();
  21. }
Reputation Points: 17
Solved Threads: 5
Junior Poster
orwell84 is offline Offline
100 posts
since Nov 2008
Dec 22nd, 2008
0

Re: Recursive Function Problem

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 22nd, 2008
0

Re: Recursive Function Problem

You're not modifying the variable, just taking it as a parameter.
Do you have to use recursion and not a loop?
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 22nd, 2008
0

Re: Recursive Function Problem

cout<<input * (input-1)<<"\n"; //multiplies it all together

that line is not dependent on the function call before it.

also, you should call your function "factorial" instead of "recurse". The recursiveness should be implied by the behavior, it shouldn't need to be stated explicitly.

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Dec 23rd, 2008
0

Re: Recursive Function Problem

I think the problem is that I don't know how to take the output of a function and use it...
Reputation Points: 17
Solved Threads: 5
Junior Poster
orwell84 is offline Offline
100 posts
since Nov 2008
Dec 23rd, 2008
0

Re: Recursive Function Problem

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:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int sumFunction(int a, int b) {
  4. return a + b;
  5. }
  6.  
  7. int main() {
  8. int a = 5, b = 7, c = 0;
  9. c = sumFunction(a, b); // c now is 5 + 7 = 12
  10. b = a + sumFunction(a, c); // b now is 5 + (5 + 12) = 22;
  11. std:cout << "123 + 123 = " << sumFunction(123, 123) << std::endl;
  12. return EXIT_SUCCESS;
  13. }

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)
  1. int factorial(int n) {
  2. if(n==0) {
  3. return 1; // 0! = 1
  4. }
  5. return /* I leave it up to you what to return here. Hint: it should contain a call to factorial(int n) itself (hence recursion) */
  6. }

Hope this helps.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 23rd, 2008
0

Re: Recursive Function Problem

Click to Expand / Collapse  Quote originally posted by orwell84 ...
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)
  1.  
  2. int iReturn = recurse (input);
  3.  
  4. or
  5.  
  6. int iReturn = 0;
  7.  
  8. 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.
Reputation Points: 13
Solved Threads: 3
Newbie Poster
Liinker is offline Offline
15 posts
since Dec 2008
Mar 23rd, 2009
0

Re: Recursive Function Problem

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)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int factorial(int x);
  5.  
  6. int main(){
  7. int x;
  8.  
  9. cout<<"Input a number to find the factorial of:\n";
  10. cin >> x;
  11. cin.ignore();
  12.  
  13. cout<<"The factorial of " << x << " is " << factorial(x) << "\n";
  14. cin.get();
  15. }
  16. int factorial(int x){
  17. if (x == 0){
  18. return 1;
  19. }
  20. return x * factorial(x - 1); //it took me MONTHS to get THIS?!?!
  21. }
Reputation Points: 17
Solved Threads: 5
Junior Poster
orwell84 is offline Offline
100 posts
since Nov 2008
Mar 23rd, 2009
0

Re: Recursive Function Problem

New problem though...(i guess it's not really a problem, just something weird) When i put in 20 to find the factorial, I get -2102132736...now I know it's not a negative number. Is this just an inherent bug in the program?
Reputation Points: 17
Solved Threads: 5
Junior Poster
orwell84 is offline Offline
100 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Flushing the input
Next Thread in C++ Forum Timeline: Getters





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


Follow us on Twitter


© 2011 DaniWeb® LLC