From 5ca32463d70b650851b26a5c3f066c3e306e5eec Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 21 Oct 2022 12:46:45 +0100 Subject: [PATCH] w3 q1 --- main.cpp | 90 ++++++++++++++++++++++++++----------------------------- stats.cpp | 21 +++++++++++++ stats.h | 9 ++++++ 3 files changed, 73 insertions(+), 47 deletions(-) create mode 100644 stats.cpp create mode 100644 stats.h diff --git a/main.cpp b/main.cpp index 35776cd..4f3c93d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,67 +1,63 @@ #include #include -#include #include +#include "stats.h" using string = std::string; //Placw in header! template -void printm(T m){ - std::cout << m; +void printm(T m) +{ + std::cout << m; } template -void printlm(T m){ - printm(m); - printm("\n"); +void printlm(T m) +{ + printm(m); + printm("\n"); } typedef std::vector::size_type vtype; double promptd(string,bool*); -int main() { - std::setprecision(3); - printlm("Calculate median and average from sequence:"); - std::vector v{}; - bool s {}; - do { - auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s); - if (s) v.push_back(d); - } - while(s); - sort(v.begin(), v.end()); - printm("The count is: "); - printlm(static_cast(v.size())); - printm("The median is: "); - int halfway {static_cast(v.size()) / 2}; - if (static_cast(v.size())%2 == 1) - printlm(v.at(static_cast(halfway))); - else - printlm((v.at(static_cast(halfway - 1)) + v.at(static_cast(halfway))) / 2.0); - double avg {}; - 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(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(); +int main() +{ + std::setprecision(3); + printlm("Calculate the average and median of scores:"); + bool s {}; + std::vector v {}; + do + { + auto d = promptd("Enter a score (Enter a non number to finish entry) : ",&s); + if (s) v.push_back(d); + } + while(s); + printm("Score median is: "); + printlm(med(v, false)); + printm("Score median (No Extremes) is: "); + printlm(med(v, true)); + printm("Score average is: "); + printlm(avg(v, false)); + printm("Score average (No Extremes) is: "); + printlm(avg(v, true)); 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; } diff --git a/stats.cpp b/stats.cpp new file mode 100644 index 0000000..b526eb3 --- /dev/null +++ b/stats.cpp @@ -0,0 +1,21 @@ +#include "stats.h" +#include +#include + +double med(std::vector &scores,bool ignoreExtremes) { + const auto len = scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0; + sort(scores.begin(), scores.end()); + using vec_size = std::vector::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 &scores,bool ignoreExtremes) { + auto sum = 0.0; + using vec_size = std::vector::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(scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0); +} diff --git a/stats.h b/stats.h new file mode 100644 index 0000000..dcc76e9 --- /dev/null +++ b/stats.h @@ -0,0 +1,9 @@ +#ifndef STATS_H_INCLUDED +#define STATS_H_INCLUDED + +#include + +double med(std::vector &scores,bool ignoreExtremes); +double avg(const std::vector &scores,bool ignoreExtremes); + +#endif // STATS_H_INCLUDED