User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,159 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,891 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 4828 | Replies: 3
Reply
Join Date: Mar 2005
Posts: 4
Reputation: koolguysj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
koolguysj koolguysj is offline Offline
Newbie Poster

Combinations(N choose R) ?

  #1  
Apr 1st, 2005
#include <stdio.h>
#include "Boolean.h"
#include "combinatorics.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 answer = *cnr;
	int multiplier;
	int divisor = 1;
	int k;

	if(n < r)
	{
		k = n;
	}
	else
	{
		k = r;
	}

	while(divisor <= k)
	{
			answer = (answer * multiplier) / divisor;
	
		    multiplier--;
		    divisor++;
	}
}

The Algorithm For N-Choose-R in High Level Pseudocode:

Let k = min (r, n-r)
Start answer = 1
Start multiplier = n
Start divisor = 1
while divisor<= k do
{ answer = ( answer * multiplier ) div divisor # this will evenly divide
decrement the multiplier
increment the divisor
}

Here is my program so far. I need help on calcCNR(n choose r) function. I'm not worried about detecting overflow yet. Need to get it to calculate correctly first. But it's returning garbage so far. The algorithm is given above. The most confusing part for me is "let k = min (r, n-r). I have attempted to code that, but not sure if it's correct. Help needed.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Location: Woodland Hills CA
Posts: 49
Reputation: aj.wh.ca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Light Poster

Re: Combinations(N choose R) ?

  #2  
Apr 1st, 2005
Yes , The problem is in your implementation of min function. In fact the #define which you have commented is correct.
Uncomment the #define and then instead of
if(n < r)
{
k = n;
}
else
{
k = r;
}
write k = min(r,n-r) ;

Do you need to take care of cases like C(5,11) ? The above may not work then. Will need to change the condition.
cheers,
aj.wh.ca

-------------------------------------------
www.swiftthoughts.com
-------------------------------------------
Reply With Quote  
Join Date: Mar 2005
Posts: 4
Reputation: koolguysj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
koolguysj koolguysj is offline Offline
Newbie Poster

Re: Combinations(N choose R) ?

  #3  
Apr 1st, 2005
Boolean calcCNR( int n, int r, int* cnr )
{
    #define min(n,r)  (((n) < (r)) ? (n) : (r));

    int answer = *cnr;
	int multiplier;
	int divisor = 1;
	int k;
   
	/*
	if(n < r)
	{
		k = n;
	}
	else
	{
		k = r;
	}
	*/

	k = min(r,n-r);
    answer = 1;
	while(divisor <= k)
	{
		    

			answer = (answer * multiplier) / divisor;
	
		    multiplier--;
		    divisor++;
	}
}


void main()
{
   int result;
	int n;
	int r;

	printf("Input two number:  ");
	scanf("%d %d", &n, &r);

	calcCNR(n, r, &result);

	printf("result of cnr is: %d\n", result);
}

Not working yet. Help please.
Reply With Quote  
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Combinations(N choose R) ?

  #4  
Apr 4th, 2005
One problem is that you'r not correctly initializing the variables in calcCNR. The last variable is where the result appears. When you call the function this variable has no significant value. I'm surprised that the compiler let you get away with the statement
"anwer = *cnr". The initial value of answer should be "answer = 1". At the end you should set *cnr = answer.

Come to think of it, why are you using indirection at all? Why not just have the function calcCNR return the value you've computed?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 8:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC