| | |
Combinations(N choose R) ?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 4
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#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
}
•
•
Join Date: Mar 2005
Posts: 53
Reputation:
Solved Threads: 1
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.
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
-------------------------------------------
aj.wh.ca
-------------------------------------------
www.swiftthoughts.com
-------------------------------------------
•
•
Join Date: Mar 2005
Posts: 4
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
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.
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
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?
"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?
![]() |
Similar Threads
- Hi, heed help to choose a new web hosting. (Web Hosting Deals)
- Advertise on Myspace (Ad Space for Sale)
- How do you choose your moderators? (Social Media and Online Communities)
- Avoid A Color Disaster! (Site Layout and Usability)
- summations and combinations (C++)
- how the user can choose the directory of files (C++)
Other Threads in the C Forum
- Previous Thread: Exercise using: unsigned char bcd(int n);
- Next Thread: Encryption of numbers....
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h





