Hello, there. I've lately started to read the 'Accelerated C++' book that my grandson recommended me, I'm a little new to some of these lower level concepts, as I've only written programs in scripting languages such as perl before.
The issue I'm having is on a part of the book that finds a median value within a vector of integers or doubles or maybe some other type of number, but we can assume integers for this purpose), the program reads from stdin, and then uses a piece of code similar to the following to find the median value..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// okay, so we make a vector and stick some data in it...
double numbers;
vector<double> numbers_vector;
while(cin >> numbers)
{
numbers_vector.push_back(numbers);
}
// okay so now we have a vector of data, then it goes onto find the median value
vector<double>::size_type numbers_size = numbers.size();
sort(numbers.begin(), numbers.end());
vector<double>::size_type half = numbers_size/2;
double median = numbers_size % 2 == 0 ? (numbers[half] + numbers[half-1]) /2
: numbers[half];
Now, to me that says, if the value is say, 7 (7 elements in the vector), then it will output an evaluation of numbers[3.5]... how do you refer to the top half of an element?