// // Program name: combination_function // Author: B. Massingill // // This program defines and tests a "combination" function. // // Input, output: see program text // #include // // Function name: combination // // Precondition: // n is positive and m is positive and m <= n // Postcondition: // return value is "n choose m" -- number of ways of choosing // m objects from among n // // Note that we don't say what this function does if the precondition // is not met. // So to use this function appropriately, you should be sure to // call it only with n and m that meet the precondition. // int combination(int n, int m); // // Function name: factorial // // Precondition: // n is positive // Postcondition: // return value is n factorial // // Note that we don't say what this function does if n is not positive. // So to use this function appropriately, you should be sure to // call it only with positive n. // int factorial(int n); // // Main program // int main() { int n, m; cout << "Enter two positive integers, the second <= the first\n"; cin >> n >> m; if (n > 0 && m > 0 && m <= n) cout << n << " choose " << m << " is " << combination(n, m) << endl; else cout << "Invalid input\n" ; return 0; } // code for factorial function (see above for function documentation) int factorial(int n) { int valueToReturn = 1 ; for (int i = 2; i <= n; i++) valueToReturn *= i; return valueToReturn; } // code for combination function (see above for function documentation) int combination(int n, int m) { return factorial(n) / (factorial(m) * factorial(n-m)); }