Combinations(N choose R) ?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 4
Reputation: koolguysj is an unknown quantity at this point 
Solved Threads: 0
koolguysj koolguysj is offline Offline
Newbie Poster

Combinations(N choose R) ?

 
0
  #1
Apr 1st, 2005
  1. #include <stdio.h>
  2. #include "Boolean.h"
  3. #include "combinatorics.h"
  4.  
  5. /* For all the functions below, return TRUE if the
  6.   calculation is successful, FALSE if overflow occurs
  7.   Return the calculated value in the pass by reference
  8.   parameter provided
  9. */
  10.  
  11. Boolean calcFactorial (int n, int* nfact)
  12. {
  13. *nfact = 1;
  14.  
  15. while(n > 0)
  16. {
  17. *nfact = *nfact * n;
  18. n--;
  19. }
  20.  
  21. if(*nfact < 0x7fffffff)
  22. {
  23. return TRUE;
  24. }
  25. else
  26. {
  27. return FALSE;
  28. }
  29. }
  30.  
  31. /*
  32. Combination means C(n,r) = n!/( r! * (n-r)! )
  33. where C(n,r) is the number of r-element subsets of an n-element set.
  34. Better formula derived from above is:
  35.   n ( n-1 ) ( n-2 ) ... ( n-r+1 )
  36.  C(n,r) = -------------------------------
  37.   r ( r-1 ) ( r-2 ) ... (3)(2)(1)
  38.  
  39.  
  40.   Return True if calculation is successful. False if
  41.   Overflow occurs.
  42. */
  43.  
  44. Boolean calcCNR( int n, int r, int* cnr )
  45. {
  46. //#define min(n,r) = (((n) < (r)) ? (n) : (r));
  47.  
  48. int answer = *cnr;
  49. int multiplier;
  50. int divisor = 1;
  51. int k;
  52.  
  53. if(n < r)
  54. {
  55. k = n;
  56. }
  57. else
  58. {
  59. k = r;
  60. }
  61.  
  62. while(divisor <= k)
  63. {
  64. answer = (answer * multiplier) / divisor;
  65.  
  66. multiplier--;
  67. divisor++;
  68. }
  69. }

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 53
Reputation: aj.wh.ca is an unknown quantity at this point 
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Junior Poster in Training

Re: Combinations(N choose R) ?

 
0
  #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 Quick reply to this message  
Join Date: Mar 2005
Posts: 4
Reputation: koolguysj is an unknown quantity at this point 
Solved Threads: 0
koolguysj koolguysj is offline Offline
Newbie Poster

Re: Combinations(N choose R) ?

 
0
  #3
Apr 1st, 2005
  1. Boolean calcCNR( int n, int r, int* cnr )
  2. {
  3. #define min(n,r) (((n) < (r)) ? (n) : (r));
  4.  
  5. int answer = *cnr;
  6. int multiplier;
  7. int divisor = 1;
  8. int k;
  9.  
  10. /*
  11. if(n < r)
  12. {
  13. k = n;
  14. }
  15. else
  16. {
  17. k = r;
  18. }
  19. */
  20.  
  21. k = min(r,n-r);
  22. answer = 1;
  23. while(divisor <= k)
  24. {
  25.  
  26.  
  27. answer = (answer * multiplier) / divisor;
  28.  
  29. multiplier--;
  30. divisor++;
  31. }
  32. }
  33.  
  34.  
  35. void main()
  36. {
  37. int result;
  38. int n;
  39. int r;
  40.  
  41. printf("Input two number: ");
  42. scanf("%d %d", &n, &r);
  43.  
  44. calcCNR(n, r, &result);
  45.  
  46. printf("result of cnr is: %d\n", result);
  47. }

Not working yet. Help please.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Combinations(N choose R) ?

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC