-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam_2.cpp
87 lines (73 loc) · 1.69 KB
/
exam_2.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <algorithm>
using namespace std;
int validateArrLength(string prompt)
{
int val;
while (true)
{
cin.clear();
cin.sync();
cout << prompt;
cin >> val;
if (cin.good() && val >= 1 && val < 100)
{
break;
}
else
cin.clear();
cout << "Invalid input! Value must be between 1 and 99" << endl;
}
return val;
}
int validateRequestsCount(string prompt)
{
int val;
while (true)
{
cin.clear();
cin.sync();
cout << prompt;
cin >> val;
if (cin.good() && val >= 1 && val < 50)
{
break;
}
else
cin.clear();
cout << "Invalid input! Value must be between 1 and 49" << endl;
}
return val;
}
istream &operator>>(istream &in, pair<int, int> &p) {
in>>p.first>>p.second;
return in;
}
int main()
{
int arrLength = validateArrLength("Enter array length: ");
int percentage;
vector<int> arr;
pair<int, int> temp;
cout << "Enter values:" << endl;
for(int val, i = 1; (cin >> val) && i < arrLength + 1; ++i){
arr.push_back(val);
if (i == arrLength)
break;
}
int requestsCount = validateRequestsCount("Enter requests count: ");
for (int i = 0; i < requestsCount; i++)
{
cin >> temp;
arr.insert(arr.begin() + temp.first - 1, temp.second);
}
cout << endl;
sort(arr.begin(), arr.end());
arrLength = arr.size();
percentage = arrLength / 4;
cout << "First 25% are: " << endl;
for (int i = 0; i < percentage; i++){
cout << arr[i] << ' ';
}
return 0;
}