Permutation?

This is a discussion on Permutation? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include "Boolean.h" #include "combinatorics.h" #include <limits.h> /* For all the functions below, return TRUE if the calculation ...

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

    Permutation?

    Code:
    #include <stdio.h>
    #include "Boolean.h"
    #include "combinatorics.h"
    #include <limits.h>
    
    /* 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)
    {
        *nfact = 1;
    
    	while(n > 0)
    	{
    	       *nfact = *nfact * n;
    		   n--;		   
    	}
    	if(*nfact < 0x7fffffff)
    		   {
    		      return TRUE;	 
    		   }
    	       else
    		   {
    		      return FALSE;
    		   }      
    }
    
    /*
    Combination means C(n,r) = n!/( r! * (n-r)! ) 
    where C(n,r) is the number of r-element subsets of an n-element set.
    Better formula derived from above is:
              n ( n-1 ) ( n-2 ) ... ( n-r+1 ) 
     C(n,r) = ------------------------------- 
              r ( r-1 ) ( r-2 ) ... (3)(2)(1) 
    
    
      Return True if calculation is successful. False if
      Overflow occurs.
    */
    
    Boolean calcCNR( int n, int r, int* cnr )
    {
        #define min(n,r)  (((n) < (r)) ? (n) : (r));
    
        int multiplier = n;
        int divisor = 1;
        int k;
        INT_MAX;
    
        k = min(r, n - r);
        //printf("n=%d, r=%d, k=%d\n", n, r, k);
        
        
    	*cnr = 1;
        while (divisor <= k) 
    	{
    		if(INT_MAX / multiplier < *cnr) 
    		{ 
    			printf("Overflow detected\n");
    		    return FALSE;
    		}
    		else
    		{
            *cnr = ((*cnr * multiplier) / divisor);
       		
            //printf("Intermediate *cnr=%d\n", *cnr);
            multiplier--;
            divisor++;
    		}
        }
    	return TRUE;
    
    }
    Boolean calcPNR( int cnr, int rfact, int* pnr )
    {
    
    }
    here is my program so far for calculating factorials, combinations, and permutations. Can someone show me an algorithm or pseudocode to calculate the calcPNR(permuation) function please?

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Problem creating a recursive string permutation function
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2006, 10:09 AM
  3. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  4. Permutation Calculation
    By Eric Hansen in forum C++ Programming
    Replies: 21
    Last Post: 06-11-2003, 04:03 PM
  5. INT ARRAY Permutation!
    By arthur5005 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21