In: Computer Science
In C++
Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd.
Ex: If the input is: 2 3 4 8 11 -1
the output is:
Middle item: 4
The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the data into a vector. Then, based on the number of items, find the middle item. 276452.1593070
( Must include vector library to use vectors)
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NUM_ELEMENTS = 9;
vector<int> userValues;
int num = 0, count = 0;
while (num != -1) {
cin >> num;
if (num != -1) {
if (count == NUM_ELEMENTS) {
cout << "Too many inputs" << endl;
return 0;
}
userValues.push_back(num);
++count;
}
}
cout << "Middle item: " << userValues[count / 2] << endl;
return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NUM_ELEMENTS = 9;
vector<int> userValues;
int num = 0, count = 0;
while (num != -1) {
cin >> num;
if (num != -1) {
if (count == NUM_ELEMENTS) {
cout << "Too many inputs" << endl;
return 0;
}
userValues.push_back(num);
++count;
}
}
cout << userValues[count / 2] << endl;
return 0;
}
