-
Notifications
You must be signed in to change notification settings - Fork 4
/
temp_stats.cpp
38 lines (31 loc) · 936 Bytes
/
temp_stats.cpp
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
36
37
38
#include <algorithm>
#include <fstream>
#include <iostream>
#include <numeric>
#include <vector>
#include "ch05/temperature_converters.h"
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Usage: " << argv[0] << " filename\n";
return 0;
}
ifstream ifs(argv[1]);
if (!ifs) {
cerr << "can't open input file " << argv[1] << endl;
return 1;
}
vector<double> temps;
int h;
double t;
char u;
while (ifs >> h >> t >> u)
temps.push_back(u == 'f' ? ftoc(t) : t);
double sum = accumulate(temps.begin(), temps.end(), 0.0);
cout << "mean temps: " << sum / temps.size() << endl;
sort(temps.begin(), temps.end());
double median = temps.size() % 2 == 1 ? temps[temps.size() / 2] :
(temps[temps.size() / 2] + temps[temps.size() / 2 - 1]) / 2;
cout << "median temps: " << median << endl;
return 0;
}