simple const function example

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

main.cpp main file

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

#include "std_lib_facilities.h"

class test {
	string label;
public:
	test(string _l) :label(_l) {};
	void print(const int); //can be used if the value is just localy used in the print function
};

void test::print(const int n) {
	cout << label << n << "\n";
}

int main()
try
{
	const int n = 2;
	test myTest("o ");
	myTest.print(n);

	keep_window_open();	
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
o 2
Please enter a character to exit

longest, shortest, and string to int

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

main.cpp main file

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

#include "std_lib_facilities.h"

struct stringVec
{
	vector<string> vs;
	vector<int> letterCount;
	vector<int> letterCountsorted;
	string longest;
	string shortest;
	string first;
	string last;

public:
	stringVec(vector<string>& _vs) :vs(_vs) {};
	void stringToInt();
	void stringSort();
	void print();
};

void stringVec::stringToInt() {
	for (string x : vs) {
		letterCount.push_back(x.size());
	}
}

void stringVec::stringSort() {
	//longest & shortest
	letterCountsorted = letterCount;
	sort(letterCountsorted);
	int min = letterCountsorted[0];
	int max = letterCountsorted[letterCountsorted.size() - 1];
	int maxIndex;
	int minIndex;
	for (int i = 0; i < letterCountsorted.size(); i++) {
		if (max == letterCount[i]) {
			maxIndex = i;
		}
		if (min == letterCount[i]) {
			minIndex = i;
		}
	}
	longest = vs[maxIndex];
	shortest = vs[minIndex];
	//lexicographical first & last 
	sort(vs);
	first = vs[0];
	last = vs[vs.size() - 1];
}

void stringVec::print() {
	
	for (int x : letterCount) {
		cout << x << "\n";
	}
	cout << "longest: " << longest << "\n";
	cout << "shortest: " << shortest << "\n";
	cout << "first: " << first << "\n";
	cout << "last: " << last << "\n";
}

int main()
try
{
	vector<string> myVec{ "hello", "how", "are", "you", "my", "name", "is" };
	stringVec a(myVec);

	a.stringToInt();

	a.stringSort();

	a.print();

	keep_window_open();

}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
5
3
3
3
2
4
2
longest: hello
shortest: is
first: are
last: you
Please enter a character to exit

print until char appears [times x]

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<string> alphabet{ "a","b","c","d",
						"e","f","g","h",
						"a","b","c","d",
						"e","f","g","h" };

void print_until_ss(vector<string>& v, string quit, int quitNum) {
	int quitCount = 0;
	for (string s : v) {
		if (s == quit && quitCount < quitNum) {
			quitCount++;
			cout << s << "\n";
		}
		else if(quitCount == quitNum) {
			return;
		}
		else {
			cout << s << "\n";
		}
	}
}

int main()
try
{
	print_until_ss(alphabet, "c", 2);
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
a
b
c
d
e
f
g
h
a
b
c
Please enter a character to exit

print until char appears

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<string> alphabet{ "a","b","c","d",
						"e","f","g","h",
						"i","j","k","l",
						"m","n","o","p",
						"q","r","s","t",
						"u","v","w","x",
						"y","z" };

void print_until_s(vector<string>& v, string quit) {
	for (string s : v) {
		if (s == quit) return;
		cout << s << "\n";
	}
}

int main()
try
{
	print_until_s(alphabet, "k");
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
a
b
c
d
e
f
g
h
i
j
Please enter a character to exit

min, max, mean value of vector

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<int> myVec{ 5,2,1,6,4 };

struct extremes
{
	vector<int> v;
	int min;
	int max;
	double mean;
	int median;
public:
	extremes(vector<int>& _v) :v(_v) {};
	void calc();
	void print();
};

void extremes::calc() {
	sort(v);
	//max
	max = v[0];
	//min
	min = v[v.size() - 1];
	//mean
	double additionIndex = 0;
	for (int x : v) {
		additionIndex += x;
	}
	mean = additionIndex / v.size();
	//median
	if (v.size() % 2 == 0)
	{
		median = (v[v.size() / 2 - 1] + v[v.size() / 2]) / 2;
	}
	else
	{
		median = v[v.size() / 2];
	}
}

void extremes::print() {
	cout << "min: " << min << "\n";
	cout << "max: " << max << "\n";
	cout << "mean: " << mean << "\n";
	cout << "median: " << median << "\n";
}

int main()
try
{
	extremes e(myVec);
	e.calc();
	e.print();
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();	
}
Output:
min: 6
max: 1
mean: 3.6
median: 4
Please enter a character to exit

max value of vector

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<int> myVec{ 1, 2, 5, 4, 1, 8, 2, 3, 4, 9 };

void maxv(vector<int>& v) {
	sort(v);
	cout << v[v.size() - 1] << "\n";
}

int main()
try
{
	maxv(myVec);

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
9
Please enter a character to exit

prices times weights

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<double> price;
vector<double> weight;

class indexCalc {
	vector<double> p;
	vector<double> w;

public:
	indexCalc(vector<double> _p, vector<double> _w) :p(_p), w(_w) { };
	void readPrice();
	void readWeight();
	void calculate();
};

void indexCalc::readPrice() {
	while (cin) {
		char priceChar;
		double priceDouble;
		cin.get(priceChar);
		if (isspace(priceChar)) {
			if (priceChar == '\n') {
				break;
			}
		}
		cin.unget();
		cin >> priceDouble;
		p.push_back(priceDouble);
	}
}

void indexCalc::readWeight() {
	while (p.size() != w.size()) {
		char weightChar;
		double weightDouble;
		cin.get(weightChar);
		if (isspace(weightChar)) {
			if (weightChar == '\n' && p.size() == w.size()) {
				break;
			}
		}
		cin.unget();
		cin >> weightDouble;
		w.push_back(weightDouble);
	}
}

void indexCalc::calculate() {
	double index = 0;
	for (int i = 0; i < p.size(); i++) {
		index += p[i] * w[i];
	}
	cout << index << "\n";
}

int main()
try
{
	indexCalc ic(price, weight);

	cout << "Enter prices\n";
	
	ic.readPrice();

	cout << "Enter weights\n";

	ic.readWeight();
	ic.calculate();

	keep_window_open();
}

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

catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
Enter prices
23 24 25 53 32
Enter weights
3 4 5 3 4
577
Please enter a character to exit

name age pairs 1.0

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

main.cpp main file

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

#include "std_lib_facilities.h"

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

class NameAge {
	vector<string> n;
	vector<int> a;
public:
	NameAge(vector<string>& _n, vector<int>& _a) :n(_n), a(_a) {};
	void readNames();
	void readAges();
	void print();
	void sortVectors();
};

void NameAge::readNames() {
	while (cin) {
		char letter;
		cin.get(letter);
		if (isalpha(letter)) {
			string name;
			name += letter;
			while (cin.get(letter) && isalpha(letter)) {
				name += letter;
			}
			n.push_back(name);
		}
		if (isspace(letter)) {
			if (letter == '\n') {
				break;
			}
		}
	}
}

void NameAge::readAges() {

	while (n.size() != a.size()) {
		char ageChar;
		int ageInt;
		cin.get(ageChar);
		if (isspace(ageChar)) {
			if (ageChar == '\n' && n.size() == a.size()) {
				break;
			}
		}
		cin.unget();
		cin >> ageInt;
		a.push_back(ageInt);
	}
}

void NameAge::print() {
	for (int i = 0; i < n.size(); i++) {
		cout << n[i] << " " << a[i] << "\n";
	}
}

void NameAge::sortVectors() {
	vector<string> tempNames = n;
	vector<int> tempAges = a;
	vector<int> index;

	sort(tempNames.begin(), tempNames.end());

	for (int i = 0; i < n.size(); i++) {
		for (int j = 0; j < n.size(); j++) {
			if (tempNames[i] == n[j]) {
				index.push_back(j);
			}
		}
	}

	for (int i = 0; i < n.size(); i++) {
		a[i] = tempAges[index[i]];
	}
	n = tempNames;
}

int main()
try
{
	NameAge na(names, ages);

	cout << "Enter name(s)\n";

	na.readNames();

	cout << "Promt ages in order of names:\n";

	na.readAges();

	na.sortVectors();

	na.print();

	keep_window_open();
}

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

catch (...) {
	cout << "Exiting\n";
	keep_window_open();

}
Output:
Enter name(s)
Philipp Alfred Isabell Benjamin
Promt ages in order of names:
28 60 31 23
Alfred 60
Benjamin 23
Isabell 31
Philipp 28
Please enter a character to exit

reverse char sequence

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<string> myVec{ "a","b","c","d","e","f","g","h","i","j" };
vector<string> myVecRev;

class Rev {
public:
	vector<string> v;
	vector<string> vr;

	Rev(vector<string>& _v, vector<string>& _vr) :v(_v), vr(_vr) {};
	void reverse();
	void reverseSame();
	void printV(string);
	void printVR(string);
};

void Rev::reverse() {
	for (int i = 0; i < v.size(); i++) {
		vr.push_back(v[(v.size() - 1) - i]);
	}
}

void Rev::reverseSame() {
	for (int i = 0; i < v.size() / 2; i++) {
		string temp;
		temp = v[i];
		v[i] = v[(v.size() - 1) - i];
		v[(v.size() - 1) - i] = temp;
	}
}

void Rev::printV(string label) {
	for (string x : v) {
		cout << label << " " << x << "\n";
	}
}

void Rev::printVR(string label) {
	for (string x : vr) {
		cout << label << " " << x << "\n";
	}
}

int main()
try
{
	string label = "l";

	Rev rev(myVec, myVecRev);

	cout << "First version:\n";

	rev.reverse();
	rev.printV(label);
	rev.printVR(label);

	cout << "Second version:\n";

	rev.reverseSame();
	rev.printV(label);

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

catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
First version:
l a
l b
l c
l d
l e
l f
l g
l h
l i
l j
l j
l i
l h
l g
l f
l e
l d
l c
l b
l a
Second version:
l j
l i
l h
l g
l f
l e
l d
l c
l b
l a
Please enter a character to exit

reverse integer sequence

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

main.cpp main file

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

#include "std_lib_facilities.h"

vector<int> myVec{ 0,1,2,3,4,5,6,7,8,9 };
vector<int> myVecRev;

class Rev {


public:
	vector<int> v;
	vector<int> vr;

	Rev(vector<int>& _v, vector<int>& _vr) :v(_v), vr(_vr) {};
	void reverse();
	void reverseSame();
	void printV(string&);
	void printVR(string&);
};

void Rev::reverse() {
	for (int i = 0; i < v.size(); i++) {
		vr.push_back(v[(v.size() - 1) - i]);
	}
}

void Rev::reverseSame() {
	for (int i = 0; i < v.size() / 2; i++) {
		int temp;
		temp = v[i];
		v[i] = v[(v.size() - 1) - i];
		v[(v.size() - 1) - i] = temp;
	}
}

void Rev::printV(string& _label) {
	for (int x : v) {
		cout << _label << " " << x << "\n";
	}
}

void Rev::printVR(string& _label) {
	for (int x : vr) {
		cout << _label << " " << x << "\n";
	}
}

int main()
try
{
	string label = "l";
	Rev rev(myVec, myVecRev);
	cout << "First version:\n";

	rev.reverse();
	rev.printV(label);
	rev.printVR(label);

	cout << "Second version:\n";
	
	rev.reverseSame();
	rev.printV(label);

	keep_window_open();

}


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

catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
First version:
l 0
l 1
l 2
l 3
l 4
l 5
l 6
l 7
l 8
l 9
l 9
l 8
l 7
l 6
l 5
l 4
l 3
l 2
l 1
l 0
Second version:
l 9
l 8
l 7
l 6
l 5
l 4
l 3
l 2
l 1
l 0
Please enter a character to exit