1
0
This commit is contained in:
Captain ALM 2022-10-21 12:46:45 +01:00
parent 453902b44e
commit 5ca32463d7
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
3 changed files with 73 additions and 47 deletions

View File

@ -1,67 +1,63 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <algorithm>
#include <iomanip> #include <iomanip>
#include "stats.h"
using string = std::string; using string = std::string;
//Placw in header! //Placw in header!
template <typename T> template <typename T>
void printm(T m){ void printm(T m)
std::cout << m; {
std::cout << m;
} }
template <typename T> template <typename T>
void printlm(T m){ void printlm(T m)
printm(m); {
printm("\n"); printm(m);
printm("\n");
} }
typedef std::vector<double>::size_type vtype; typedef std::vector<double>::size_type vtype;
double promptd(string,bool*); double promptd(string,bool*);
int main() { int main()
std::setprecision(3); {
printlm("Calculate median and average from sequence:"); std::setprecision(3);
std::vector<double> v{}; printlm("Calculate the average and median of scores:");
bool s {}; bool s {};
do { std::vector<double> v {};
auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s); do
if (s) v.push_back(d); {
} auto d = promptd("Enter a score (Enter a non number to finish entry) : ",&s);
while(s); if (s) v.push_back(d);
sort(v.begin(), v.end()); }
printm("The count is: "); while(s);
printlm(static_cast<int>(v.size())); printm("Score median is: ");
printm("The median is: "); printlm(med(v, false));
int halfway {static_cast<int>(v.size()) / 2}; printm("Score median (No Extremes) is: ");
if (static_cast<int>(v.size())%2 == 1) printlm(med(v, true));
printlm(v.at(static_cast<vtype>(halfway))); printm("Score average is: ");
else printlm(avg(v, false));
printlm((v.at(static_cast<vtype>(halfway - 1)) + v.at(static_cast<vtype>(halfway))) / 2.0); printm("Score average (No Extremes) is: ");
double avg {}; printlm(avg(v, true));
if (v.size() > 2) {
for (vtype i = 1; i < v.size() - 1; ++i) avg += v.at(i);
} else {
for (vtype i = 0; i < v.size(); ++i) avg += v.at(i);
}
avg /= static_cast<int>(v.size());
printm("The average is: ");
printlm(avg);
return 0;
}
double promptd(string message, bool *status) {
double tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0; return 0;
} }
*status = true;
return tr; double promptd(string message, bool *status)
{
double tr {0.0};
*status = false;
std::cout << message;
if (!(std::cin >> tr))
{
std::cin.clear();
std::cin.ignore();
return 0.0;
}
*status = true;
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