I\'m a programming student, and for a project I\'m working on, on of the things I have to do is compute the median value of a vector of int values. I\'m to do this using onl
I'm not exactly sure what your restrictions on the user of member functions of vector are, but index access with []
or at()
would make accessing elements simpler:
median = hWScores.at(hWScores.size() / 2);
You can also work with iterators like begin() + offset
like you are currently doing, but then you need to first calculate the correct offset with size()/2
and add that to begin()
, not the other way around. Also you need to dereference the resulting iterator to access the actual value at that point:
median = *(hWScores.begin() + hWScores.size()/2)