/* Dataset.cpp
 *
 */
 
#include "Dataset.h"
#include <cctype>       // isalpha()
#include <numeric>      // accumulate()
#include <algorithm>    // sort()
using namespace std;

Dataset::Dataset(const string& fileName)
{
  ifstream fin( fileName.data() );
  assert( fin.is_open() );
                              // initialize myTitle
  char ch = fin.peek();       // (if present)
  if ( isalpha(ch) ) 

  {
     getline(fin, myTitle);

	 fin.get();

  }
  else
     myTitle = "Untitled data set";
                              // read data
  double aValue;
  for (;;)
  {
    fin >> aValue;
    
    if ( fin.eof() ) break;
    
    myValues.push_back(aValue);
  }
  
  fin.close();
}

double Dataset::getMean() const
{
  assert( !myValues.empty() );
  double sum = accumulate(myValues.begin(), myValues.end(), 0.0);
  return sum/myValues.size();
}

inline bool even(int n);

bool even(int n)
{
  return n % 2 == 0;
}

double Dataset::getMedian() const
{
  vector<double> copy = myValues;
  sort( copy.begin(), copy.end() );

  int mid = copy.size() / 2,     // integer division
      mid1 = mid - 1;
  
  if ( even( copy.size() ) )        
      return (copy[mid] + copy[mid1]) / 2;
  else
    return copy[mid];
}

