I am trying to write a second function to calculate a vector of integers which is in the main function. My vector is set up like this.
int inputinfo;
cout << "\nPlease enter in scores: ";
cout << "\nEnd your input with ctrl-z\n";
vector<int> scores;
while (cin >> inputinfo)
{
scores.push_back(inputinfo);
}
Here is my median equation (which I am not sure is working right). I would like to make a function for median and then call it back to the main function to find the median of the vector.
double median;
size_t size = scores.size();
sort(scores.begin(), scores.end());
if (size % TWO == 0)
{
median = (scores[size / 2 - 1] + scores[size / 2]) / 2;
}
else
{
median = scores[size / 2];
}
Thanks for any help.
Check whether your code fails if thee is none or only one number in the vector. You can fix this using
if (size==0) throw "Vector empty";
if (size==1) return scores[0];
before the if (size % TWO == 0) line.