0

Hi all.

I'm working my way through "Accelerated C++" have a question regarding chapter 5.

The program calculates students final grades by taking a median of the homework grades.

I am trying to alter this program so that it uses *list* instead of *vector*.

The issue I'm having is with the median function. The function selected values from the middle of the *vector* but on changing it to *list* I am struggling to make it work.

How could I select the middle value of the *list*?

Many thanks.

```
// source file for median function
#include <algorithm>
#include <stdexcept>
#include <list>
using std::domain_error; using std::sort; using std::list;
double median(list<double> vec)
{
typedef list<double>::size_type vec_sz;
vec_sz size = vec.size();
if(size==0) throw domain_error("median of an empty list");
sort(vec.begin(),vec.end());
vec_sz mid=size/2;
for(list<double>::const_iterator it=vec.begin(); it!=vec.end(); ++it) // Start of "list" median calculation
{
if(it == mid)
{
return size%2==0 ? (vec[*it]+vec[*it-1])/2 : vec[*it];
}
} // End of "list" median calculation
/*return size%2==0 ? (vec[mid]+vec[mid-1])/2 : vec[mid];*/ // Calculation when using "vector"
}
```