name and age pairs [name called by age]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 21

#include "std_lib_facilities.h"

int main()
try
{
	vector<string> names;
	string nameTemp;

	vector<int> ages;
	int ageTemp;
	int ageInput;
	int ageCheck = 0;

	bool check = true;

	while (check == true) {
		cout << "Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.\n";

		if (cin >> nameTemp >> ageTemp) {
			if (nameTemp == "noname" && ageTemp == 0) {
				check = false;
			}
			else {
				for (int i = 0; i < names.size(); i++) {
					if (names[i] == nameTemp) {
						error("This name already exists ", nameTemp);
					}
				}
				names.push_back(nameTemp);
				ages.push_back(ageTemp);
			}
		}
		else {
			error("Bad input");
		}
	}

	cout << "Enter a integer to look for\n";
	cin >> ageInput;

	for (int i = 0; i < ages.size(); i++) {
		if (ages[i] == ageInput) {
			cout << "You entered: " << ageInput << " . Corresponded name is " << names[i] << "\n";
			ageCheck++;
		}
	}

	if (ageCheck == 0) {
		cout << ageInput << " . Could not be found in the list of ages.\n";
	}

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting \n";
	keep_window_open(".");
}
Output:
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
Peter 20
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
Hans 20
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
noname 0
Enter a integer to look for
20
You entered: 20 . Corresponded name is Peter
You entered: 20 . Corresponded name is Hans
Please enter . to exit

name and age pairs [age called by name]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 20

#include "std_lib_facilities.h"

int main()
try
{
	string nameTemp;
	string nameInput;
	int nameCheck = 0;

	int ageTemp;

	vector<string> names;
	vector<int> ages;

	bool check = true;

	while (check == true) {
		cout << "Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.\n";

		if (cin >> nameTemp >> ageTemp) {
			if (nameTemp == "noname" && ageTemp == 0) {
				check = false;
			}
			else {
				for (int i = 0; i < names.size(); i++) {
					if (names[i] == nameTemp) {
						error("This name already exists ", nameTemp);
					}
				}
				names.push_back(nameTemp);
				ages.push_back(ageTemp);
			}
		}
		else {
			error("Bad input");
		}
	}

	cout << "Enter a name to look for\n";
	cin >> nameInput;

	for (int i = 0; i < names.size(); i++) {
		if (names[i] == nameInput) {
			cout << "You entered: " << nameInput << " . Corresponded score is " << ages[i] << "\n";
			nameCheck++;
		}
	}

	if (nameCheck == 0) {
	cout << nameInput << " . Could not be found in the list of names.\n";
	}
	
	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting \n";
	keep_window_open(".");
}
Output:
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
Hans 20
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
Peter 30
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
noname 0
Enter a name to look for
Peter
You entered: Peter . Corresponded score is 30
Please enter . to exit

storing name and age pairs

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 19

#include "std_lib_facilities.h"

int main()
try
{
	string nameTemp;
	string nameInput;
	int nameCheck = 0;

	int ageTemp;
	
	vector<string> names;
	vector<int> ages;
	
	bool check = true;

	while (check == true) {
		cout << "Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.\n";

		if(cin >> nameTemp >> ageTemp){
			if (nameTemp == "noname" && ageTemp == 0) {
				check = false;
			}
			else {
				for (int i = 0; i < names.size(); i++) {
					if (names[i] == nameTemp) {
						error("This name already exists ", nameTemp);
					}
				}
				names.push_back(nameTemp);
				ages.push_back(ageTemp);
			}
		}
		else {
			error("Bad input");
		}
	}
	for (int i = 0; i < names.size(); i++) {
		cout << "Name: " << names[i] << " Score: " << ages[i] << "\n";
	}

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting \n";
	keep_window_open(".");
}
Output:
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
Peter 20
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
Hans 30
Enter name and age pairs to be collected, or 'noname' and '0' to end the entry.
noname 0
Name: Peter Score: 20
Name: Hans Score: 30
Please enter . to exit

solving quadratic equations

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 18

#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;

	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.
13 6 9
Roots are complex and different.
imagined part + real part: x1 = 0.568639
imagined part - real part: x2 = -1.03018
Please enter . to exit

series [min max mode]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 17

#include "std_lib_facilities.h"

int main()
try
{
	vector<int> seriesNum;
	int singleNum;
	int counterTemp = 0;
	int countHighest = 0;
	int indexHighest = 0;

	string noNum = " ";
	bool check1 = true;

	cout << "Enter a series of numbers equal or larger to one, enter 0 to end the series" << "\n";

	while (check1 == true) {
		cout << "Enter number for series\n";
		cin.clear();

		if (cin >> singleNum) {
			if (singleNum > 0) {
				seriesNum.push_back(singleNum);
			}
			else if (singleNum == 0) {
				check1 = false;
				break;
				cout << "test";
			}

		}
		else {
			cin.clear();
			cin >> noNum;
			error("Entry was not a number", noNum);
		}
	}

	sort(seriesNum);

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

	for (int i = 1; i < seriesNum.size(); i++) {
		if (seriesNum[i] == seriesNum[i - 1]) {
			counterTemp++;
		}
		else {
			counterTemp = 0;
		}
		if (counterTemp > countHighest) {
			indexHighest = i - 1;
			countHighest = counterTemp;
		}
	}

	cout << "\nThe mode of numSeries is: " << seriesNum[indexHighest] << " counted " << countHighest + 1 << " times\n";
	cout << "The min value of numSeries is: " << seriesNum[0] << "\n";
	cout << "The max value of numSeries is: " << seriesNum[seriesNum.size()-1] << "\n";

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting!\n";
	keep_window_open(".");
}
Output:
Enter a series of numbers equal or larger to one, enter 0 to end the series
Enter number for series
2
Enter number for series
3
Enter number for series
3
Enter number for series
2
Enter number for series
2
Enter number for series
25
Enter number for series
4
Enter number for series
3
Enter number for series
0
2 2 2 3 3 3 4 25
The mode of numSeries is: 2 counted 3 times
The min value of numSeries is: 2
The max value of numSeries is: 25
Please enter . to exit

series [mode number]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 16

#include "std_lib_facilities.h"

int main()
try
{
	vector<int> seriesNum;
	int singleNum;
	int counterTemp = 0;
	int countHighest = 0;
	int indexHighest = 0;

	string noNum = " ";
	bool check1 = true;

	cout << "Enter a series of numbers equal or larger to one, enter 0 to end the series" << "\n";

	while (check1 == true) {
		cout << "Enter number for series\n";
		cin.clear();

		if (cin >> singleNum) {
			if (singleNum > 0) {
				seriesNum.push_back(singleNum);
			}
			else if (singleNum == 0) {
				check1 = false;
				break;
				cout << "test";
			}
			
		}
		else {
			cin.clear();
			cin >> noNum;
			error("Entry was not a number", noNum);
		}
	}

	sort(seriesNum);

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

	for (int i = 1; i < seriesNum.size(); i++) {
		if (seriesNum[i] == seriesNum[i - 1]) {
			counterTemp++;
		}
		else {
			counterTemp = 0;
		}
		if (counterTemp > countHighest) {
			indexHighest = i-1;
			countHighest = counterTemp;
			
		}
	}

	cout << "The Mode of numSeries is: " << seriesNum[indexHighest] << " counted " << countHighest+1 << " times\n";

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch(...) {
	cout << "Exiting!\n";
	keep_window_open(".");
}
Output:
Enter a series of numbers equal or larger to 1, enter 0 to end the series
Enter number for series
2
Enter number for series
4
Enter number for series
5
Enter number for series
3
Enter number for series
4
Enter number for series
4
Enter number for series
3
Enter number for series
2
Enter number for series
0
2 2 3 3 4 4 4 5 The Mode of numSeries is: 4 counted 3 times
Please enter . to exit

Sieve of Eratosthenes [prime count]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 15

#include "std_lib_facilities.h"
#include "math.h"

int main()
try
{
	int max = 100;
	int num = 5;
	int counter = 0;
	string s;
	vector<int> seriesNum;

	cout << "Enter a maximum value for the prime number generator: \n";
	if (cin >> max) {}
	else {
		cin.clear();
		cin >> s;
		error("Bad input for maximum", s);
	}

	cout << "How many prime numbers do you want to display? \n";
	if (cin >> num) {}
	else {
		cin.clear();
		cin >> s;
		error("Bad input for maximum", s);
	}

	//fill series of numbers
	for (int i = 2; i < max; i++) {
		seriesNum.push_back(i);
	}

	//check for primes
	for (int j = 2; j < 6; j++) {
		for (int i = 0; i < seriesNum.size() - 1; i++) {
			if (seriesNum[i] % j == 0 && seriesNum[i] != j) {
				seriesNum[i] = 0;
			}
		}
	}

	//print primes
	for (int i = 0; i < seriesNum.size(); i++) {
		if (seriesNum[i] != 0 && counter < num) {
			cout << seriesNum[i] << "\n";
			counter++;
		}
	}

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting.\n";
	keep_window_open(".");
}
Output:
Enter a maximum value for the prime number generator:
100
How many prime numbers do you want to display?
10
2
3
5
7
11
13
17
19
23
29
Please enter . to exit

Sieve of Eratosthenes [prime max]

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

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

#include "std_lib_facilities.h"
#include "math.h"

int main()
try
{
	int max = 100;
	string s;
	vector<int> seriesNum;

	cout << "Enter a maximum value for the prime number generator: \n";
	if (cin >> max){}
	else {
		cin.clear();
		cin >> s;
		error("Bad input for maximum", s);
	}

	//fill series of numbers
	for (int i = 2; i < max; i++) {
		seriesNum.push_back(i);
	}

	//check for primes
	for (int j = 2; j < 6; j++) {
		for (int i = 0; i < seriesNum.size(); i++) {
			if (seriesNum[i] % j == 0 && seriesNum[i] != j) {
				seriesNum[i] = 0;
			}
		}
	}

	//print primes
	for (int i = 0; i < seriesNum.size(); i++) {
		if (seriesNum[i] != 0) {
			cout << seriesNum[i] << "\n";
		}
	}

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Exiting.\n";
	keep_window_open(".");
}
Output:
Enter a maximum value for the prime number generator:
25
2
3
5
7
11
13
17
19
23
Please enter . to exit

Sieve of Eratosthenes

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

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

#include "std_lib_facilities.h"
#include "math.h"

int main() {

	int max = 100;
	vector<int> seriesNum;

	//fill series of numbers
	for (int i = 2; i < max; i++) {
		seriesNum.push_back(i);
	}

	//check for primes
	for (int j = 2; j < 6; j++) {
		for (int i = 0; i < seriesNum.size() - 1; i++) {
			if (seriesNum[i] % j == 0 && seriesNum[i] != j) {
				seriesNum[i] = 0;
			}
		}
	}
	
	//print primes
	for (int i = 0; i < seriesNum.size(); i++) {
		if (seriesNum[i] != 0) {
			cout << seriesNum[i] << "\n";
		}
	}

	keep_window_open(".");
}
Output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
49
53
59
61
67
71
73
77
79
83
89
91
97
99

prime number generator [max]

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

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

#include "std_lib_facilities.h"

int main() {

	int max = 100;
	vector<int> seriesNum;
	vector<int> primeNum;

	cout << "Enter max for prime number generator: \n";
	cin >> max;

	for (int i = 2; i < max; i++) {
		int count = 0;
		for (int j = 2; j < i; j++) {
			if (i % j == 0) {
				count++;
			}
		}
		if (count == 0) {
			primeNum.push_back(i);
		}
	}

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

	keep_window_open(".");
}
Output:
Enter max for prime number generator:
25
2
3
5
7
11
13
17
19
23
Please enter . to exit