STL and <algorithm> <numeric> libs

1) Write a template function EraseAll which
receives a vector<EltType> vec and an EltType elt
postcondition: all elements in vec which are equal to elt have been erased.
algorithm: uses STL library functions whenever possible.

2) Write a function DiffFromAverage which
receives a list<double> lst
action: computes the average of the elements
outputs the difference between the largest element and the average
outputs the difference between the smallest element and the average
algorithm: does not contain a loop; instead uses STL library functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template <typename EltType>
void EraseAll(vector<EltType> & vec, EltType elt)
{
  vector<EltType>::iterator itr;
  while (itr = find(vec.begin(), vec.end(), elt)
    {
      if (itr != vec.end())
         vec.erase(itr);
     }
   cout << "Vector erased" ;
}

template <typename T>
double DiffFromAverage(list<double> lst)
{
  for (int i = 0; i < lst.end(); i++)
    {
      double count = count(lst.begin(), lst.end(), 0)
      double avg = sum / count;

      double min = min_element(lst.begin(), lst.end())
      double max = max_element(lst.begin(), lst.end())
      double diffOne = (max - average);
      double diffTwo = (min - average);

     //display the differences
    cout << "Difference between max value and average is: "
         << diffOne 
         << endl;

    cout << "Difference between min value and average is: "
         << diffTwo
         << endl;
}


Did I do this ok? I felt like a couple of the comparisons i made in those functions were probably illegal but idk...
1. http://en.wikipedia.org/wiki/Erase-remove_idiom

2. Isn't the average the sum of all elements divided by the number of elements?
http://www.sgi.com/tech/stl/accumulate.html

Have you considered sorting and picking off the first and last?
http://www.cplusplus.com/reference/stl/list/sort/

You appear to have a loop.
Topic archived. No new replies allowed.