// Hagopian doing PPP6 ex 10
/*
A permutation is an ordered subset of a set. For example, say you
wanted to pick a combination to a vault. There are 60 possible
numbers, and you need three different numbers for the combination.
There are P(60,3) permutations for the combination, where P is
defined by the formula
P(60,3) = 60!/(60-3)!
where ! is used as a suffix factorial operator. For example,
4! is 4*3*2*1.
Combinations are similar to permutations, except that the order
of the objects doesn’t matter. For example, if you were making
a “banana split” sundae and wished to use three different
flavors of ice cream out of five that you had, you wouldn’t
care if you used a scoop of vanilla at the beginning or the end;
you would still have used vanilla. The formula for combinations is
C(a,b) = P(a,b)/b!
Design a program that asks users for two numbers, asks them whether
they want to calculate permutations or combinations, and prints out
the result. This will have several parts. Do an analysis of the
above requirements. Write exactly what the program will have to do.
Then, go into the design phase. Write pseudo code for the program,
and break it into sub-components.
This program should have error checking. Make sure that all
erroneous inputs will generate good error messages.
*/
#include
using namespace std;
uint64_t factorial(uint64_t n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}
uint64_t Permutations(uint64_t, uint64_t);
uint64_t Combinations(uint64_t, uint64_t);
int main() {
//Get Perm/Comb choice
//Get a and b
//print output of Permutation or Cominations
uint64_t a{ 0 }, b{ 0 };
char choice;
do {
cout << "We can compute Permutations (P) or Combinations (C).\m"
<< "\nWhich do you want? ";
cin >> choice;
} while (toupper(choice) != 'C' && toupper(choice) != 'P');
cout << "Enter a and b: ";
cin >> a >> b;
if (toupper(choice) == 'P') {
cout << "Permuations(" << a << "," << b << ") = " << Permutations(a, b) << endl;
}
else {
cout << "Combinations(" << a << "," << b << ") = " << Combinations(a, b) << endl;
}
}
uint64_t Permutations(uint64_t a, uint64_t b) {
cout << "hi mom\n";
uint64_t accumulator = 1;
if (a < b) cerr << "Can't choose more than you got.\n";
while (a >= b) {
accumulator *= a;
cout << "a - b = " << a - b << endl;
--a;
cin.get();
}
//cout << "\naccumulator = " << accumulator;
return accumulator;
}
uint64_t Combinations(uint64_t a, uint64_t b) {
return Permutations(a, b) / factorial(b);
}