Merge branch 'master' of https://code.mrmelon54.com/alfred/LearningCPP
This commit is contained in:
commit
ca5e6e8331
@ -33,6 +33,8 @@
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="stats.cpp" />
|
||||
<Unit filename="stats.h" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
|
1
main.cpp
1
main.cpp
@ -47,3 +47,4 @@ string prompts(const string &message, bool *success) {
|
||||
*success = true;
|
||||
return tr;
|
||||
}
|
||||
|
||||
|
21
stats.cpp
Normal file
21
stats.cpp
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user