/* vectorIO.h provides generic I/O functions for vector<T> objects.
 * ...
 */

#include <iostream>    // istream, ostream
#include <vector>      // vector
#include <numeric>     // accumulate()
using namespace std;

/* read() fills a vector<T> with input from a stream.
 *
 * Receives:     type parameter T,
 *               an istream and a vector<T>
 * Input: a sequence of T values
 * Precondition: operator >> is defined for type T
 * Passes back:  the modified istream and the modified vector<T>
 *****************************************************************/

template <class T>
void read(istream & in, vector<T> & theVector)
{
   T inputValue;

   for (;;)
   {
      in >> inputValue;
      if ( in.eof() ) break;
      theVector.push_back(inputValue);
   }
}

/* print() writes a vector<T> to a stream.
 *
 * Receives:     type parameter T
 *               an ostream and a vector<T>
 * Output: each T value stored in theVector to ostream out
 * Precondition: operator << is defined for type T
 * Passes back:  the modified ostream
*****************************************************************/

template <class T>
void print(ostream & out, const vector<T> & theVector)
{
   for (int i = 0; i < theVector.size(); i++)
      out << theVector[i] << ' ';
}


/* mean() finds the mean value in a vector<double>.
 *
 * Receive:      vec, a vector<double>
 * Precondition: vec is not empty
 * Return:       the mean of the values in vec
 * Note: Must #include <numeric> to use accumulate()
 ***************************************************************/

inline double mean(const vector<double>& vec);

double mean(const vector<double>& vec)
{
   if ( vec.empty() )
   {
      cerr << "\n***Mean: empty vector received!\n" << endl;
      return 0.0;
   }
   else
         return accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
}

