Need help turn this into a recursive function

Thread Solved

Join Date: Sep 2008
Posts: 35
Reputation: rrreeefff is an unknown quantity at this point 
Solved Threads: 0
rrreeefff rrreeefff is offline Offline
Light Poster

Need help turn this into a recursive function

 
0
  #1
Sep 26th, 2008
I created this function and I need help turning this into a recursive function
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3.  
  4. int fact (int n); // function prototype//
  5.  
  6. int main (void)
  7. {
  8. int n,result;
  9.  
  10. printf("please enter a positive integer");
  11. scanf("%d", &n);
  12.  
  13. result = fact(n);
  14.  
  15. printf("The answer is %d\n", result);
  16.  
  17. return 0;
  18. }
  19.  
  20. int fact(int n)
  21. {
  22. int i, product=1;
  23.  
  24. for(i=1; i<=n; i++) //algorithm
  25. {
  26. product *= i;
  27. }
  28.  
  29. return (product);
  30. }
Thanks for the guidance
Last edited by Narue; Sep 27th, 2008 at 9:04 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Need help turn this into a recursive function

 
0
  #2
Sep 26th, 2008
Perhaps you should make an effort to search on the form. There might be like tons of results on this issues. But here is the pesudo code.

  1. <return type of int> function name FACT ( taking int as an argument )
  2. {
  3. if n equals to zero
  4. return one;
  5. else
  6. return n multiple FACT ( n minus one );
  7. }

NB: Learn to start using code tags!

ssharish
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: rrreeefff is an unknown quantity at this point 
Solved Threads: 0
rrreeefff rrreeefff is offline Offline
Light Poster

Re: Need help turn this into a recursive function

 
0
  #3
Sep 27th, 2008
Is this correct for a recursive function I still have the following error (23) : error C2447: '{' : missing function header (old-style formal list?)
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3.  
  4. int fact (int n); // function prototype//
  5.  
  6. int main (void)
  7. {
  8. int n,result;
  9.  
  10. printf("please enter a positive integer");
  11. scanf("%d", &n);
  12. result = fact(n);
  13. printf("The answer is %d\n", result);
  14. return 0;
  15. }
  16.  
  17. int return1 (int n);
  18. {
  19. if (n<=1)
  20. return1;
  21. else
  22. return(n*fact(n-1));
  23. }
Last edited by cscgal; Sep 27th, 2008 at 8:04 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Need help turn this into a recursive function

 
0
  #4
Sep 27th, 2008
int return1 (int n);

Why is that semicolon there??? Delete that! Syntax error

ssharish
Last edited by ssharish2005; Sep 27th, 2008 at 4:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: rrreeefff is an unknown quantity at this point 
Solved Threads: 0
rrreeefff rrreeefff is offline Offline
Light Poster

Re: Need help turn this into a recursive function

 
0
  #5
Sep 27th, 2008
Originally Posted by ssharish2005 View Post
int return1 (int n);

Why is that semicolon there??? Delete that! Syntax error

ssharish
When deleting the semicolon the following errors shows up error LNK2019: unresolved external symbol "int __cdecl fact(int)" (?fact@@YAHH@Z) referenced in function _main
C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\lab 8.1\Debug\lab 8.1.exe : fatal error LNK1120: 1 unresolved externals
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Need help turn this into a recursive function

 
0
  #6
Sep 27th, 2008
Originally Posted by ssharish2005 View Post
int return1 (int n);

Why is that semicolon there??? Delete that! Syntax error

ssharish
And why is it called return1??? It should be fact!!!
Last edited by Sci@phy; Sep 27th, 2008 at 4:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Need help turn this into a recursive function

 
0
  #7
Sep 27th, 2008
Well there are few thing which you need to consider. First of all the function name is not write. In you code you have declared a function prototype clearly, but why dosn;t that reflect in your function definiation.

Your function name is return1 which should be return1. And then within the function your should check for n <= 0 not 1 or n == 0 . And also if thats true you should return 1 not return1 . Give a space between return and 1.

ssharish
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: Need help turn this into a recursive function

 
-1
  #8
Sep 28th, 2008
Here it is...

  1.  
  2.  
  3. #include "stdio.h"
  4. #include "conio.h"
  5. int fact (int n); // function prototype//
  6.  
  7. int main (void)
  8. {
  9. int n,result;
  10.  
  11. printf("please enter a positive integer");
  12. scanf("%d", &n);
  13.  
  14. result = fact(n);
  15.  
  16. printf("The answer is %d\n", result);
  17.  
  18. getch();
  19. return 0;
  20. }
  21.  
  22. int fact (int n)
  23. {
  24. if (n<=1)
  25. return 1;
  26. else
  27. return(n*fact(n-1));
  28. }
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 35
Reputation: rrreeefff is an unknown quantity at this point 
Solved Threads: 0
rrreeefff rrreeefff is offline Offline
Light Poster

Re: Need help turn this into a recursive function

 
0
  #9
Sep 28th, 2008
how could I add something to the function so it calculates when and if n is negative
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Need help turn this into a recursive function

 
0
  #10
Sep 28th, 2008
Isn't negative factorial kind of wrong altogether?
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