UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

n choose k

The Task

How many ways can you pick k items out of a set of n items?

                    n!
                ----------
                 k!*(n-k)!

The Program

/********************************************** * File: combine.c * Author: D. Frey * Date: 8/2/99 * Section: 101 * EMail: frey@cs.umbc.edu * * Modified by: Sue Evans * Date: 9/15/04 * * This program tests a function to compute the * mathematical combination function * Combinations(n, k), which gives the number * of ways to choose a subset of k objects from * a set of n distinct objects. Shows reuse of * previously written function, Factorial(). * Illustrates importance of acquiring a library * of often-used functions. **********************************************/ #include <stdio.h> /* Function prototypes */ int Combinations (int n, int k); int Factorial (int n); /* Main program */ int main () { int n, k; /* input n and k from the user */ printf("Enter number of objects in the set (n)? "); scanf ("%d", &n); printf("Enter number to be chosen (k)? "); scanf ("%d", &k); /* calculate and display ways to choose k from n */ printf("C(%d, %d) = %d\n", n, k, Combinations(n, k)); return 0; } /********************************************** * Function: Combinations * Usage: ways = Combinations(n, k); * * Inputs: n -- the number of distinct objects * in the set * k -- the number of objects to choose * from the set * Ouput: Number of ways to choose k from n * see description below * * Assumptions: * 1 <= n * 1 <= k <= n * * Implements the Combinations function, which * returns the number of distinct ways of choosing * k objects from a set of n objects. In * mathematics, this function is often written as * C(n,k), but a function called C is not very * self-descriptive, particularly in a language * which has precisely the same name. Implements * the formula n!/(k! * (n - k)!) **********************************************/ int Combinations(int n, int k) { int numerator, denominator, ways; /* Breaks down the calculation into easily understood parts */ numerator = Factorial (n); denominator = Factorial (k) * Factorial (n - k); ways = numerator / denominator; return (ways); } /********************************************** * Function: Factorial * Usage: f = Factorial(n); * * Input: integer n for which to calculate n! * Ouput: n! = n * n - 1 * n - 2 * ... * 3 * 2 * 1 **********************************************/ int Factorial (int n) { int product, i; product = 1; /* finds product of all integers from 1 up to and including n, where product will finally hold the value of n! */ for (i = 1; i <= n; i++) { product *= i; } return (product); }

The Sample Run

linux3[97] % a.out Enter number of objects in the set (n)? 6 Enter number to be chosen (k)? 2 C(6, 2) = 15 linux3[98] % !a a.out Enter number of objects in the set (n)? 3 Enter number to be chosen (k)? 2 C(3, 2) = 3 linux3[99] % !a a.out Enter number of objects in the set (n)? 12 Enter number to be chosen (k)? 4 C(12, 4) = 495 linux3[100] %

The Lesson


CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:13:53 EDT