Public feed
#532579
// This program fills a vector with numbers.// Then it uses this vector to calculate the median of that vector#include "mediantemplate.h"#include <iostream>#include <vector>#include <stdexcept>using std::cout;using std::cin;using std::endl;using std::vector;using std::domain_error;int main(){ cout << "Hello, please enter a series of integers." << endl; cout << "To end the series use <Ctrl-z> or EOF. " << endl; cout << "_________________________________________" << endl; cout << ": "; int value; vector<int> ivec; while (cin >> value) { ivec.push_back(value); } int med = 0; try { med = median(ivec); cout << "The median of the numbers you entered is: " << med << "\n\n\n" << endl; } catch (domain_error e) { cout << e.what(); } return 0;}#ifndef GUARD_mediantemplate_h#define GUARD_mediantemplate_h#include <vector>#include <algorithm>template <class T>T median(std::vector<T> v){ typedef typename std::vector<T>::size_type vec_sz; vec_sz size = v.size(); if (size == 0) throw domain_error("median of an empty vector"); std::sort(v.begin(), v.end()); vec_sz mid = size/2; return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];}#endif