1
0
This commit is contained in:
Captain ALM 2023-08-20 22:21:02 +01:00
commit ca5e6e8331
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
4 changed files with 33 additions and 0 deletions

View File

@ -33,6 +33,8 @@
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="stats.cpp" />
<Unit filename="stats.h" />
<Extensions /> <Extensions />
</Project> </Project>
</CodeBlocks_project_file> </CodeBlocks_project_file>

View File

@ -47,3 +47,4 @@ string prompts(const string &message, bool *success) {
*success = true; *success = true;
return tr; return tr;
} }

21
stats.cpp Normal file
View File

@ -0,0 +1,21 @@
#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);
}

9
stats.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef STATS_H_INCLUDED
#define STATS_H_INCLUDED
#include <vector>
double med(std::vector<double> &scores,bool ignoreExtremes);
double avg(const std::vector<double> &scores,bool ignoreExtremes);
#endif // STATS_H_INCLUDED