Thread: Factorial function ?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    Factorial function ?

    Code:
    /* For all the functions below, return TRUE if the
       calculation is successful, FALSE if overflow occurs
       Return the calculated value in the pass by reference
       parameter provided
    */
    
    Boolean calcFactorial (int n, int* nfact)
    {
    	unsigned long int count = 1;
    	unsigned long int result = 1;
    
    	while(count <= n)
    	{
    		if(*nfact <= 0)  // not sure about this.
    		{
    			return FALSE;
    		}
    		else
    		{
    			result = result * count;
    
    			count++;
    		}
    	}
    	return TRUE;
    }
    Factorial is: n! = n * (n-1) * (n-2) * . . . * 3 * 2 * 1
    Can someone please check if my coding is correct, especially the "if(*nfact <= 0)" which is checking for overflow ?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    it won't be checking for it as nFact is a pointer to a unknown int. You either need to be putting the factorial number in *nFact or checking that result is larger than its previous value. Checking if its negative may not nessarily work.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Code:
    /* For all the functions below, return TRUE if the
       calculation is successful, FALSE if overflow occurs
       Return the calculated value in the pass by reference
       parameter provided
    */
    
    Boolean calcFactorial (int n, int* nfact)
    {
    	unsigned long int count = 1;
    	unsigned long int result = 1;
    
    	while(n > 0)
    	{
    		*nfact = result;
    
    		if(*nfact <= 0)  // check for overflow ?
    		{
    			return FALSE;
    		}
    		else
    		{
    			result = result * count;
    
    			count++;
    		}
    	}
    	return TRUE;
    }
    How is my code so far ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    Code:
    bool fact(int n)
    {
            long count= 1;
            long result =1;
            
             while(n > -1)
             {
             if(n==0
             return 1;
             else
             result=result * n--;
             }
    return 0;
    }
    i dont think u need *nfact in your code. check out my code which finds the factorial of a number where i send just a argumnet which is nothing but the actual number.

    s.s.harish

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    the problem is, this is part of my assignment and im not suppose to change any parameters

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    This may or may not be what you are looking for:

    Code:
    Boolean calcFactorial (int n, int* nfact)
    {
    	int count = n;
            *nfact = 1;
    
    	while(count > 0)
    	{
    	       *nfact *= count;
    		count--;
    	}
    	return TRUE;
    }
    
    int main(int argc, char **argv)
    {
    	int result;
    	int i;
    
    	printf("Input a number: ");
    	scanf("%d",&i);
    
    	calcFactorial(i,&result);
    
    	printf("Result of factorial was: %d",result);
    	
    	return 0;
    }
    Basically what it does is have a value (result) to be passed in by reference to the factorial function. Then simply start at the n and multiply this by nfact itself until count is equal to 0. I also initialised nfact to 1 in case the variable wasn't initialised previously (which would return garbage if you were to use it before it was initialised).

    Also for your original code, you didn't do anything useful in where you stored the calculations. Storing it in result which only has a local scope in the function means that the value will "disappear" after the function loop ends. You could just return result be since you need to have the same parameters as what you specified before this is not really feasible.
    Last edited by 0rion; 04-01-2005 at 05:25 AM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by 0rion
    You could just return result be since you need to have the same parameters as what you specified before this is not really feasible.
    The return value is supposed to be used to determine whether overflow would have occurred, and thus whether any value calculated is valid or not.

    [edit]I thought this sounded familiar.
    Last edited by Dave_Sinkula; 04-01-2005 at 01:56 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.*

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Code:
    Boolean calcFactorial (int n, int* nfact)
    {
        *nfact = 1;
    
    	while(n > 0)
    	{
    	       *nfact = *nfact * n;
    		   n--;	   
    	}
    
    	if(*nfact < 0x7fffffff)
    	{
    		return TRUE;
    	}
    	else
    	{
    		return FALSE;
    	}
    }
    Thank you Orion. Modify it a bit, but works great.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Not if you want to detect overflow.
    Code:
    #include <stdio.h>
    
    typedef enum { FALSE, TRUE } Boolean;
    
    Boolean calcFactorial (int n, int* nfact)
    {
        *nfact = 1;
    
    	while(n > 0)
    	{
    	       *nfact = *nfact * n;
    		   n--;	
    	}
    
    	if(*nfact < 0x7fffffff)
    	{
    		return TRUE;
    	}
    	else
    	{
    		return FALSE;
    	}
    }
    
    int main()
    {
       int value, result;
       for ( value = 0; value < 20; ++value )
       {
          if ( calcFactorial(value,&result) )
          {
             printf("%2d! = %d\n", value, result);
          }
       }
       return 0;
    }
    
    /* my output
     0! = 1
     1! = 1
     2! = 2
     3! = 6
     4! = 24
     5! = 120
     6! = 720
     7! = 5040
     8! = 40320
     9! = 362880
    10! = 3628800
    11! = 39916800
    12! = 479001600
    13! = 1932053504
    14! = 1278945280
    15! = 2004310016
    16! = 2004189184
    17! = -288522240
    18! = -898433024
    19! = 109641728
    */
    [edit]An expected output would be:
    Code:
     0! = 1
     1! = 1
     2! = 2
     3! = 6
     4! = 24
     5! = 120
     6! = 720
     7! = 5040
     8! = 40320
     9! = 362880
    10! = 3628800
    11! = 39916800
    12! = 479001600
    Last edited by Dave_Sinkula; 04-01-2005 at 05:03 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.*

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    The overflow detecting part i can worry later. Right now i just want to work on the correctless of the calculation parts. I'm currectly working on my other functions that work together with this function. Hope all goes well.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by koolguysj
    The overflow detecting part i can worry later. Right now i just want to work on the correctless of the calculation parts.
    You realize that overflow detection is directly related to the correctness?
    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.*

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Heres an one that checks for overflow that uses the implementation from the other forum you posted in:

    Code:
    #include <limits.h>
    #include <stdio.h>
    
    // your other definitions goes here..
    
    Boolean calcFactorial (int n, int* nfact)
    {
    	int count = n;
    	*nfact = 1;
    	
    	while(count > 0)
    	{
    		if (*nfact >= INT_MAX / count)
    			return FALSE; 
    		*nfact *= count;
    		count--;
    	}
    	return TRUE;
    }
    
    int main(int argc, char **argv)
    {
    	int result;
    	int i;
    	int j;
    
    	printf("Input a number: ");
    	scanf("%d",&i);
    
    	j = calcFactorial(i,&result);
    
            if (j == FALSE)
    	{
    		fprintf(stderr,"Sorry the input was too large!");
    		return 1;
    	}
    
    	printf("Result of factorial was: %d",result);
    	
    	return 0;
    }
    You simply just have to check the return value and see if it overflowed or not, so if it overflowed then you simply can print a error message to the user saying it was too big or something (what ever you want to do with it).
    Last edited by 0rion; 04-01-2005 at 11:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM