# C Homework Assignment: Compute choose function

Design and write a complete C program including adequate comments to compute C(n, r).  This function is known as the choose function, and is defined as the number of r element subsets of an n element set.  So C(5, 2) = 10 because there are ten possible two element subsets from a set of 5 elements.  Similarly, C(4, 1) = 4, C(4, 4) = 1, and C(4, 0) = 1.

Mathematically the choose function is defined as:

C(n, r) = n! / (R! * (n - r)! )

Where n and r are positive integers (or zero), and "!" means factorial, so C(5, 2) = 5! / (2! * 3!) = 120 / (2 * 6) = 120 / 12 = 10.  Note that 0! (zero factorial) is defined to be 1 (one).

You must use exactly and only the following four functions in your program whose prototypes are as shown, and each performs the following tasks only:

• int main ( void );
Prompts the user to enter a pair of integers from one line of input and stores them in variables n and r respectively;
Call the function check() to make sure they are each non-negative and that n <= r, exiting the program if not;
Next, call the function choose() to calculate and return the result;
Finally, print the result to the standard output.
• int check ( int n, int r );
Verify that both n and r are >= 0, and that n >= r.  If so, return 1; if not, print an appropriate error message to standard error and return 0.
• unsigned long fact ( int num );
Compute and return num! (that is, the factorial of num).
• unsigned long choose ( int n, int r );
Compute and return C( n, r ).

The program should compile perfectly (without errors) and execute without error.

## Extra-Credit:

The number of possible 5-card poker hands is C( 52, 5 ), which equals 2598960.  Yet even thought this value will fit into a long (or a 4 byte int), it is likely that trying to compute C( 52, 5 ) will cause an error or a system crash.  (If you don't get a problem on that, try C( 52, 13 ) which is the number of possible bridge hands.)  Why?  Indicate how you would fix the problem.  (It's not necessary to provide a second program, just briefly write in English what you would do.)

 Send comments and mail to pollock@acm.org.