#include using namespace std; int factorial(int); void choose(int,int,int&); int main() { int n, k, result; n = 10; k = 1; result = 0; choose(n, k, result); cout << "n: " << n << endl; cout << "k: " << k << endl; cout << "result: " << result << endl; return 0; } // // Description: calculates x! // Pre: x is greater than or equal to zero // Post: returns x factorial // int factorial(int x) { int result = 1; if (x < 0) { cout << "ERROR: (x < 0) in factorial(x)" << endl; assert(x >= 0); } if (x == 0) { return 1; } // We know (x > 0) while (x > 1) { result = result * x; x--; } return result; } // // Description: calculates N choose K // Pre: n and k are both greater than or equal to 0 // Post: myResult contains the value of N choose K // // NOTE: THERE'S A BUG HERE! // void choose(int n, int k, int& myResult) { assert((n >= 0) && (k >= 0)); k = 2; myResult = factorial(n) / (factorial(k) * factorial(n - k) ); return; }