Thread: Problem with Recursion calculing Factorials

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Problem with Recursion calculing Factorials

    Well, I've been learning those things that I didn't knew in theC tutorial of this site, and, in the recursion chapter, there was a homework where you had to write a program which calculates factorials.

    This is my function:
    Code:
    int ObtenerFactorial( int num, int count, int result )
    
    {
    	if ( count==num ) return result;
    	else
    		{
    			result *= (num-count);
    			ObtenerFactorial( num, count+1, result );
    		}
    }
    I call the function from main this way:
    Code:
    fflush(stdin);
    scanf( "%d", &number );
    factorial = ObtenerFactorial( (int)number, 0, 1 );
    printf( "\nFactorial de %d: %d\n", number, factorial);
    Where 'number' is entered throw the command line.

    When the user enter 0, the result is fine, 1. But when you enter any number greater than 0 (1,5,etc), everytimes get out the same result: 1075144832. I don't know what is the problem... I don't expect that you write my homework, just to tell what's wrong: if it's all wrong, I'll write a new program.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You meant return ObtenerFactorial( num, count+1, result );

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I call the function from main this way:
    You started out with the first line of your code being wrong. That's usually a bad way to start.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Thanks for your help, but I don't understand:

    FIRST: no, I meant return result;, because if the number is equal to zero, it returns one, and if the count is equal to, for example, 5, in a factorial you don't have "5!=0*1*2*3*4*5", there is no a zero.

    SECOND: I really don't understand. That is a part of the main function, if you are refering to the "#include... int main...", if it's not that, I don't know what could be.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > Explanations of... > Why fflush(stdin) is wrong

    [edit]FAQ > How do I... (Level 1) > How do I get a number from the user (C)
    Last edited by Dave_Sinkula; 12-12-2005 at 04:50 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Err.. wait. I am not 100% sure here, for my memory of recursion is rusty. However, only your outermost call will return. It returns to the call that called it. You don't save that value, and it is lost. So first poster is right, your line
    Code:
    ObtenerFactorial( num, count+1, result );
    should be
    Code:
     return ObtenerFactorial( num, count+1, result );
    That way, first call returns the results of the second call who returns the results of the third call etc., and you get the results in your variable. Now, you get no results in it, and thus it's undefined. Try setting factorial to 0, you'll get 0 as answer.

    edit: As an addendum, the other posters are also right, so don't flush stdin and such.
    Last edited by Silfer; 12-13-2005 at 09:25 AM.
    -S

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    factorial = ObtenerFactorial( (int)number, 0, 1 );
    you dont need to type cast it in this. its not need.#

    ssharish2005

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    yeah, later I could see that it's not needed a cast because scanf( "%d", &number) receive only decimals.
    I change that line 'ObtenerFact...' for 'return ObtenerFact...', and it works now, thanks you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM