Website Summary Files Commits Branches @commit @diff @stats
path: root / Week10 / Exercise12.cpp
blob: 38bf452eb66b366645764d9899fff7742bba1e14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

int main() {
	unsigned int count = 0, allocated = 4;
	int* indecies = new int[allocated];
	double* values = new double[allocated];

	int index;
	std::cin >> index;
	while (index >= 0) {
		indecies[count] = index;
		std::cin >> values[count++];

		if (count >= allocated) {
			allocated *= 2;
			int* newIndecies = new int[allocated];
			double* newValues = new double[allocated];

			for (unsigned int i = 0; i < count; i++) {
				newIndecies[i] = indecies[i];
				newValues[i] = values[i];
			}

			delete[] indecies;
			delete[] values;

			indecies = newIndecies;
			values = newValues;
		}

		std::cin >> index;
	}

	std::cin >> index;
	bool printed = false;

	for (unsigned int i = 0; i < count; i++) {
		if (indecies[i] == index) {
			std::cout << values[i] << std::endl;
			printed = true;
			break;
		}
	}

	if (!printed)
		std::cout << 0 << std::endl;

	delete[] indecies;
	delete[] values;

	return 0;
}