// G. Hagopian-- PPP4 exercise 2

/*
2. If we define the median of a sequence as “a number so that exactly 
as many elements come before it in the sequence as come after it,” 
fix the program in §4.6.3 so that it always prints out a median. 
Hint: A median need not be an element of the sequence.
*/

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;

const double secperday = 24 * 60 * 60;

vector<double> createVector(int n) {
	vector<double> vd;
	for (int i = 0; i < n; ++i)
		vd.push_back(rand() / 100.);
	return vd;
}

double median(vector<double> v) {
	sort(v.begin(), v.end());
	if (v.size() % 2 == 1) return v[v.size() / 2];
	else return (v[v.size() / 2 - 1] + v[v.size() / 2]) / 2;
}

int main() {
	srand(time(0));
	cout << "The time now is " << time(0)/secperday << " days since 1/1/70\n";
	vector<double> randomVector = createVector(4);
	for (double d : randomVector) cout << d << " ";
	cout << "\nThe median is " << median(randomVector);
}