1
0
LearningCPP/stats.cpp

22 lines
898 B
C++
Raw Normal View History

2022-10-21 12:46:45 +01:00
#include "stats.h"
#include <vector>
#include <algorithm>
double med(std::vector<double> &scores,bool ignoreExtremes) {
const auto len = scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0;
sort(scores.begin(), scores.end());
using vec_size = std::vector<double>::size_type;
const vec_size middle = (vec_size) (len / 2);
if (middle%2 == 1)
return scores[middle];
else
return (scores[middle - 1] + scores[middle]) / 2.0;
}
double avg(const std::vector<double> &scores,bool ignoreExtremes) {
auto sum = 0.0;
using vec_size = std::vector<double>::size_type;
for (vec_size i = (ignoreExtremes && scores.size() > 2) ? 1 : 0; i < scores.size() - (ignoreExtremes && scores.size() > 2) ? 1 : 0; i++) sum += scores[i];
return sum / static_cast<double>(scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0);
}