permutations and combinations

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 6 Exercise 10
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 6 Exercise 10

/*
input:
12
3
c
*/

#include "std_lib_facilities.h"

int factorial(int _n) {
	int fact = 1;
	while (1 < _n){
		fact *= _n;
		--_n;
		if (fact < 1) {
			error("factorial overflow");
		}
	}
	return fact;
}

int permutations(int _a, int _b) {
	if (_a < _b || _a < 1 || _b < 1) {
		error("bad permutation sizes");
	}
	return factorial(_a) / factorial(_a - _b);
}

int combinations(int _a, int _b)
{
	return permutations(_a, _b) / factorial(_b);
}

int main()
try
{
	int a;
	int b;
	string aCheck;
	string bCheck;
	char answer;
	bool permCheck = false;
	bool combCheck = false;

	cout << "Enter two integer values for permutation and combination: \n";
	cout << "Enter value for a\n";

	if (cin >> a) {}
	else{
		cin.clear();
		cin >> aCheck;
		error("Wrong value for a: ", aCheck);
	}

	cout << "Enter value for b\n";
	if (cin >> b) {}
	else {
		cin.clear();
		cin >> aCheck;
		error("Wrong value for b: ", aCheck);
	}
	if (b > a) {
		error("Value b must be smaller than a, b: ", b);
	}

	cout << "a = " << a << "\n";
	cout << "b = " << b << "\n";

	cout << "Enter 'p' for permutations, 'c' for combinations or 'b' for both:\n";
	cin >> answer;
	if (answer == 'p') {
		permCheck = true;
	}
	else if (answer == 'c') {
		combCheck = true;
	}
	else if (answer == 'b') {
		permCheck = true;
		combCheck = true;
	}
	else {
		error("Bad input", answer);
	}

	if (permCheck) {
		cout << "Permutation: " << permutations(a, b) << "\n";
	}
	if(combCheck){
		cout << "Combination: " << combinations(a, b) << "\n";
	}

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}

Output:

Enter two integer values for permutation and combination:
Enter value for a
12
Enter value for b
3
a = 12
b = 3
Enter 'p' for permutations, 'c' for combinations or 'b' for both:
c
Combination: 220
Please enter . to exit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.