week day values

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 14

#include "std_lib_facilities.h"

int main()
try
{
	vector<string> dayVec = { "Monday", "monday", "Mon",
							"Tuesday", "tuesday", "Tue",
							"Wednesday", "wednesday", "Wed",
							"Thursday", "thursday", "Thu",
							"Friday", "friday", "Fri",
							"Saturday", "saturday", "Sat",
							"Sunday", "sunday", "Sun" };
	vector<int> dayVal = { 0,0,0,0,0,0,0 };
	string day;
	int value;
	string falsevalue;
	bool check = true;

	cout << "Enter pairs of week-days and values seperated by blank space: \n";
	cout << "Enter exit 0 to end entry.\n";

	while(check){
		if(cin >> day >> value){
			for(int i = 0; i < dayVec.size(); i++){
				if (day == dayVec[i]) {
					dayVal[i / 3] += value;
				}
			}
			if (day == "exit" && value == 0) {
				break;
				check = true;
			}
		}
		else {
			cin.clear();
			cin >> falsevalue;
			error("Entered value illegal: ", falsevalue);
		}
	}
	
	for (int i = 0; i < dayVal.size(); i++) {
		cout << dayVec[i*3] << ": " << dayVal[i] << "\n";
	}
	
	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
Enter pairs of week-days and values seperated by blank space:
Enter exit 0 to end entry.
Mon 3 Wednesday 6 friday 3 exit 0
Monday: 3
Tuesday: 0
Wednesday: 6
Thursday: 0
Friday: 3
Saturday: 0
Sunday: 0
Please enter . to exit

bulls and cows number guessing game [random seed]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 13

/*
input:
3
4630
y
2
4567
4567
6275
n
*/

#include "std_lib_facilities.h"

int main()
try
{
	int seed;
	string checkSeed;

	int rndNum;
	int rndNumCount = 4;
	vector<int> rndNums;

	int guess;
	int bulls = 0;
	int cows = 0;
	vector<int> guessVec;

	string answer = "noAnswer";
	
	while (rndNums.size() < rndNumCount) {
		cout << "Enter seed\n";
		if (cin >> seed) {}
		else {
			cin.clear();
			cin >> checkSeed;
			error("Entered seed is not a number: ", checkSeed);
		}

		for (int i = 0; i < rndNumCount;) {
			if (i == 0) {
				rndNum = (rand() % 100) / 10;
				rndNums.push_back(rndNum);
				i++;
			}
			else {
				rndNum = (rand() % 100) / 10;
				if (rndNum != rndNums[i - 1]) {
					rndNums.push_back(rndNum);
					i++;
				}
			}
		}

		for (int i = 0; i < rndNums.size(); i++) {
			cout << "rnd num: " << rndNums[i] << "\n";
		}
	
		cout << "Enter 4 different digit number each ranging from 0 to 9 (for example 4567):\n";
		while (bulls != rndNumCount) {
			bulls = 0;
			cows = 0;
			if (cin >> guess) {
				if (guess / 1000 >= 1 && guess / 1000 < 10) {
	
					guessVec.push_back(guess / 1000 % 10);
					guessVec.push_back(guess / 100 % 10);
					guessVec.push_back(guess / 10 % 10);
					guessVec.push_back(guess / 1 % 10);

					for (int i = 0; i < guessVec.size() - 1; i++) {
						if (guessVec[i] == guessVec[i + 1]) {
							error("Enter 4 different numbers.");
						}
					}

					for (int i = 0; i < guessVec.size(); i++) {
						for (int j = 0; j < guessVec.size(); j++) {
							if (rndNums[j] == guessVec[i] && i == j) {
								bulls++;
							}
							else if (rndNums[j] == guessVec[i]) {
								cows++;
							}
						}
					}
				}
				else {
					error("Enter a 4 digit number.");
				}
			}
			else {
				error("Enter a number.");
			}

			cout << "Bulls: " << bulls << " Cows: " << cows << "\n";
			guessVec.clear();

		}
		cout << "You win!\n";

		cout << "Play again? Press y or n: \n";
		cin >> answer;

		if (answer == "y") {
			rndNums.clear();
			bulls = 0;
			//break;
		}
		else {
			break;
		}
	}

	keep_window_open(".");
}

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

Enter seed
3
rnd num: 4
rnd num: 6
rnd num: 3
rnd num: 0
Enter 4 different digit number each ranging from 0 to 9 (for example 4567):
4630
Bulls: 4 Cows: 0
You win!
Play again? Press y or n:
y
Enter seed
2
rnd num: 6
rnd num: 2
rnd num: 7
rnd num: 5
Enter 4 different digit number each ranging from 0 to 9 (for example 4567):
4567
Bulls: 0 Cows: 3
4567
Bulls: 0 Cows: 3
6275
Bulls: 4 Cows: 0
You win!
Play again? Press y or n:
n
Please enter . to exit

bulls and cows number guessing game

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 12

//input: 4621
//input: 4567
//input: 1234

#include "std_lib_facilities.h"

int main()
try
{
	int guess;
	int bulls = 0;
	int cows = 0;

	vector<int> guessVec;
	vector<int> numToBeguessed{ 1,2,3,4 };

	cout << "Enter 4 different digit number each ranging from 0 to 9 (for example 4567):\n";
	while (bulls != 4) {
		bulls = 0;
		cows = 0;
		if (cin >> guess) {
			if (guess / 1000 >= 1 && guess / 1000 < 10) {
			
				guessVec.push_back(guess / 1000 % 10);
				guessVec.push_back(guess / 100 % 10);
				guessVec.push_back(guess / 10 % 10);
				guessVec.push_back(guess / 1 % 10);
			
				for (int i = 0; i < guessVec.size()-1; i++) {
					if (guessVec[i] == guessVec[i + 1]){
						error("Enter 4 different numbers.");
					}
				}

				for (int i = 0; i < guessVec.size(); i++) {
					for (int j = 0; j < guessVec.size(); j++) {
						if (numToBeguessed[j] == guessVec[i] && i == j) {
							bulls++;
						}
						else if (numToBeguessed[j] == guessVec[i]) {
							cows++;
						}
					}
				}
			}
			else {
				error("Enter a 4 digit number.");
			}
		}
		else {
		error("Enter a number.");
		}

		cout << "Bulls: " << bulls << " Cows: " << cows << "\n";
		guessVec.clear();
	}

	cout << "You win!\n";
	keep_window_open(".");
}

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

Enter 4 different digit number each ranging from 0 to 9 (for example 4567):
4621
Bulls: 0 Cows: 3
4567
Bulls: 0 Cows: 1
3120
Bulls: 0 Cows: 3
1234
Bulls: 4 Cows: 0
You win!
Please enter . to exit

largest number in fibonacci series

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 11

#include "std_lib_facilities.h"

int main()
try
{
	vector<int> fibonacciNum{ 0,1 };
	bool fibBool = true;
	int counter = 0;

	while(fibBool){
		int nextFibNum = fibonacciNum[counter] + fibonacciNum[counter + 1];
		
		if (nextFibNum >= 0) {
			fibonacciNum.push_back(nextFibNum);
		}
		else {
			fibBool = false;
		}
		counter++;
	}

	for (int i = 0; i < fibonacciNum.size(); i++) {
		cout << fibonacciNum[i] << "\n";
	}

	cout << fibonacciNum.size() << " fibonacci integers fit into a int vector\n";
	cout << "The largest number in the int vector is: " << fibonacciNum[fibonacciNum.size() - 1] << "\n";

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
47 fibonacci integers fit into a int vector
The largest number in the int vector is: 1836311903

sum of integer range [differences]

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

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

//input: 3
//input: 1 2 3

#include "std_lib_facilities.h"

int main()
try
{
	vector<double> numVec;
	vector<double> numDiffs;

	int numCount = 0;
	double value;
	double numSum = 0;
	string check;

	cout << "Enter number of integers to be read:\n";
	if (cin >> numCount) {}
	else {
		cin.clear();
		cin >> check;
		error("Entered value does not match: ", check);
	}

	cout << "Enter some values to be stored seperated by a blank space "
		"ending with a 'x': \n";
	for (int i = 0; i < numCount; i++) {
		if (cin >> value) {
			numVec.push_back(value);
			numSum += value;
		}
		else {
			cin.clear();
			cin >> check;
			if (check == "x") {
				break;
			}
			else {
				error("Input is no value: ", check);
			}
		}
	}

	if (numVec.size() < numCount) {
		error("The entered number of integers to be read is larger than the vector size.\n");
	}

	for (int i = 0; i < numVec.size(); i++) {
		cout << numVec[i] << "\n";
	}

	for (int i = 0; i < numVec.size() - 1; i++) {
		numDiffs.push_back(numVec[i + 1] - numVec[i]);
		cout << "Differences: " << numDiffs[i] << "\n";
	}

	cout << "sum = " << numSum << "\n";

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
Enter number of integers to be read:
3
Enter some values to be stored seperated by a blank space ending with a 'x':
1
2
3
1
2
3
Differences: 1
Differences: 1
sum = 6
Please enter . to exit

sum of integer range [no doubles]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 9

//input: 3
//input: 1.2 4 3.2 5x

#include "std_lib_facilities.h"

int main()
try
{
	vector<double> numVec;

	int numCount = 0;
	double value;
	double numSum = 0;
	string check;

	cout << "Enter number of integers to be read:\n";
	if (cin >> numCount) {}
	else {
		cin.clear();
		cin >> check;
		error("Entered value does not match: ", check);
	}

	cout << "Enter some values to be stored seperated by a blank space "
		"ending with a 'x': \n";
	for (int i = 0; i < numCount; i++) {
		if (cin >> value) {
			numVec.push_back(value);
			numSum += value;
		}
		else {
			cin.clear();
			cin >> check;
			if (check == "x") {
				break;
			}
			else {
				error("Input is no value: ", check);
			}
		}
	}

	if (numVec.size() < numCount) {
		error("The entered number of integers to be read is larger than the vector size.\n");
	}

	for (int i = 0; i < numVec.size(); i++) {
		cout << numVec[i] << "\n";
	}

	int numSum_int = numSum;
	double numSum_check = numSum - numSum_int;

	if (numSum_check > 0.0) {
		cout << "unfortunately the sum cannot be written as integer" << "\n";
	}
	else {
		cout << "sum = " << numSum << "\n";
	}

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
Enter number of integers to be read:
3
Enter some values to be stored seperated by a blank space ending with a 'x':
1.2 4 3.2 5x
1.2
4
3.2
unfortunately the sum cannot be written as integer
Please enter . to exit

sum of integer range

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 8

//input: 5
//input: 3 5 6 3 34 54x

#include "std_lib_facilities.h"

int main()
try
{
	vector<int> numVec;
	
	int numCount = 0;
	int value;
	int numSum = 0;
	string check;

	cout << "Enter number of integers to be read:\n";
	if(cin >> numCount){}
	else {
		cin.clear();
		cin >> check;
		error("Entered value does not match: ", check);
	}

	cout << "Enter some values to be stored seperated by a blank space "
		"ending with a 'x': \n";
	for (int i = 0; i < numCount; i++) {
		if (cin >> value) {
			numVec.push_back(value);
			numSum += value;
		}
		else {
			cin.clear();
			cin >> check;
			if (check == "x") {
				break;
			}
			else{
				error("Input is no value: ", check);
			}
		}
	}

	if (numVec.size() < numCount) {
		error("The entered number of integers to be read is larger than the vector size.\n");
	}

	for (int i = 0; i < numVec.size(); i++) {
		cout << numVec[i] << "\n";
	}
	cout << "sum = " << numSum << "\n";

	keep_window_open(".");
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
Enter number of integers to be read:
5
Enter some values to be stored seperated by a blank space ending with a 'x':
3 5 6 3 34 54x
3
5
6
3
34
sum = 51
Please enter . to exit

quadratic equation

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 7

//input: 1 2 3

#include "std_lib_facilities.h"

int main()
try
{
	float a, b, c, x1, x2, determinant, realNum, imagineNum;

	cout << "Enter a value for 'a', 'b' and 'c' to solve a quadratic equation.\n";

	if (cin >> a >> b >> c) {}
	else {
		error("Bad input for a, b or c");
	}

	determinant = pow(b, 2) - 4 * a * c;
	cout << "for a = " << a << ", b = " << b << ", c = " << c << "\ndeterminant = " << determinant << "\n";

	if (determinant > 0) {
		x1 = (-b + sqrt(determinant)) / (2 * a);
		x2 = (-b - sqrt(determinant)) / (2 * a);

		cout << "Roots are real and different.\n";
		cout << "x1 = " << x1 << "\n";
		cout << "x2 = " << x2 << "\n";
	}

	else if (determinant == 0) {
		x1 = (-b + sqrt(determinant)) / (2 * a);

		cout << "Roots are real and same.\n";
		cout << "x1 = x2 " << x1 << "\n";
	}

	else {
		realNum = -b / (2 * a);
		imagineNum = sqrt(-determinant) / (2 * a);

		cout << "Roots are complex and different.\n";
		cout << "imagined part + real part: x1 = " << realNum + imagineNum << "\n";
		cout << "imagined part - real part: x2 = " << realNum - imagineNum << "\n";
	}

	keep_window_open(".");
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
Enter a value for 'a', 'b' and 'c' to solve a quadratic equation.
1
2
3
for a = 1, b = 2, c = 3
determinant = -8
Roots are complex and different.
imagined part + real part: x1 = 0.414214
imagined part - real part: x2 = -2.41421
Please enter . to exit

celsius to kelvin and fahrenheit

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

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

//input: 24

#include "std_lib_facilities.h"

double ctoftok(double c) {

	//celsius to fahrenheit to kelvin

	double lowestTempinC = -273.15;
	double highestTempinC = 57.8;
	double kelvin = 273.15;

	if (lowestTempinC >= c) {
		error("Temperature is unrealisticly low: ", c);
	}
	if (highestTempinC <= c) {
		error("Temperature is unrealisticly high: ", c);
	}

	double f = 9.0 / 5 * c + 32;
	double k = c + kelvin;

	cout << "celsius: " << c << ", fahrenheit: " << f << ", kelvin: " << k << "\n";
	return 0;
}

int main()
try
{
	double value = 0;
	string s;

	cout << "Enter temperature in celsius degree to get fahrenheit and kelvin: \n";

	if (cin >> value) {
		ctoftok(value);
	}

	else {
		cin.clear();
		cin >> s;
		error("Illegal input entered: ", s);
	}

	keep_window_open();
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting \n";
	keep_window_open(".");
}
Output:
Enter temperature in celsius degree to get fahrenheit and kelvin:
24
celsius: 24, fahrenheit: 75.2, kelvin: 297.15

celsius to kelvin and vice versa

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 5

//input: 24c
//input: 24k

#include "std_lib_facilities.h"

double ctok(double c) {

	double lowestTempinC = -273.15;
	double highestTempinC = 57.8;
	double kelvin = 273.15;

	if (lowestTempinC >= c) {
		error("Temperature is unrealisticly low: ", c);
	}
	if (highestTempinC <= c) {
		error("Temperature is unrealisticly high: ", c);
	}

	double k = c + kelvin;

	if (k >= (lowestTempinC + kelvin) && k <= (highestTempinC + kelvin)) {
		return k;
	}
	else {
		error("post-condition ctok()");
	}
}

double ktoc(double k) {

	double lowestTempinK = 0;
	double highestTempinK = 330.95;
	double kelvin = 273.15;

	if (lowestTempinK >= k) {
		error("Temperature is unrealisticly low: ", k);
	}
	if (highestTempinK <= k) {
		error("Temperature is unrealisticly high: ", k);
	}

	double c = k - kelvin;

	if (c >= (lowestTempinK - kelvin) && c <= (highestTempinK - kelvin)) {
		return c;
	}
	else {
		error("post-condition ktoc()");
	}
}

int main()
try
{
	double value = 0;
	string unit;
	string s;

	cout << "Enter temperature with a c for celsius or k for kelvin, to be converted vis a vis: \n";

	if (cin >> value >> unit) {
		if(unit == "c"){
			double k = ctok(value);
			cout << value << " celsius are " << k << " kelvin" << '\n';
		}
		else if(unit == "k") {
			double c = ktoc(value);
			cout << value << " kelvin are " << c << " celsius" << '\n';
		}
		else {
			error("unit entered does not exist: ", unit);
		}
	}

	else {
		cin.clear();
		cin >> s;
		error("Illegal input entered: ", s);
	}

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting \n";
	keep_window_open(".");
}
Output:
Enter temperature with a c for celsius or k for kelvin, to be converted vis a vis:
24c
24 celsius are 297.15 kelvin

Enter temperature with a c for celsius or k for kelvin, to be converted vis a vis:
24k
24 kelvin are -249.15 celsius