Recursive Function Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Recursive Function Problem

 
0
  #1
Dec 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Recursive Function Problem

 
0
  #2
Dec 22nd, 2008
And I guess it would help if I put the code in here...

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,361
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Recursive Function Problem

 
0
  #3
Dec 22nd, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Recursive Function Problem

 
0
  #4
Dec 22nd, 2008
You're not modifying the variable, just taking it as a parameter.
Do you have to use recursion and not a loop?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Recursive Function Problem

 
0
  #5
Dec 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Recursive Function Problem

 
0
  #6
Dec 23rd, 2008
I think the problem is that I don't know how to take the output of a function and use it...
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Recursive Function Problem

 
0
  #7
Dec 23rd, 2008
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:
  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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: Recursive Function Problem

 
0
  #8
Dec 23rd, 2008
Originally Posted by orwell84 View Post
I think the problem is that I don't know how to take the output of a function and use it...
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Recursive Function Problem

 
0
  #9
Mar 23rd, 2009
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 70
Reputation: orwell84 is an unknown quantity at this point 
Solved Threads: 3
orwell84's Avatar
orwell84 orwell84 is offline Offline
Junior Poster in Training

Re: Recursive Function Problem

 
0
  #10
Mar 23rd, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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