From 59df0a6adb0688bbd7c0a2cb625d3e06bddcd133 Mon Sep 17 00:00:00 2001 From: hrajpoot-js <65027195+hrajpoot-js@users.noreply.github.com> Date: Fri, 6 May 2022 14:39:21 +0530 Subject: [PATCH 1/3] Update stats.h --- stats.h | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/stats.h b/stats.h index 237da47..bc2a905 100644 --- a/stats.h +++ b/stats.h @@ -1,5 +1,46 @@ #include +#include -namespace Statistics { - Stats ComputeStatistics(const std::vector<___>& ); +class Stats +{ + public: + double min; + double max; + double average; + + Stats(double imin, double imax, double iaverage): min(imin), max(imax), average(iaverage) {} +}; + +namespace Statistics +{ + Stats ComputeStatistics(const std::vector& input); } + + +class IAlerter +{ + public: + bool emailSent = false; + bool ledGlows = false; +}; + +class EmailAlert : public IAlerter +{ +}; + +class LEDAlert : public IAlerter +{ +}; + +class StatsAlerter +{ + private: + const float aMaxThreshold; + std::vector aAlerters; + + public: + StatsAlerter(const float iMaxThreshold, std::vector iAlerters): + aMaxThreshold(iMaxThreshold), aAlerters(iAlerters) { } + + void checkAndAlert(std::vector iInput); +}; From 7715b9148105fec2f769ae9515d378abbd072d0a Mon Sep 17 00:00:00 2001 From: hrajpoot-js <65027195+hrajpoot-js@users.noreply.github.com> Date: Fri, 6 May 2022 14:40:05 +0530 Subject: [PATCH 2/3] Update stats.cpp --- stats.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/stats.cpp b/stats.cpp index 1af50ce..d5f1241 100644 --- a/stats.cpp +++ b/stats.cpp @@ -1,5 +1,43 @@ #include "stats.h" -Stats Statistics::ComputeStatistics(const std::vector<___>& ) { - //Implement statistics here +Stats Statistics::ComputeStatistics(const std::vector& input) +{ + double aMin; + double aMax; + double aAverage; + double aSum; + + if(input.empty()) + { + aAverage = nan("1"); + aMin = nan("1"); + aMax = nan("1"); + Stats aStat(aMin, aMax, aAverage); + return aStat; + } + + //input vector is non empty, Implement statistics here + aMin = *min_element(input.begin(), input.end()); + aMax = *max_element(input.begin(), input.end()); + + for(double elem : input) + { + aSum += elem; + } + + aAverage = aSum/input.size(); + + Stats aStat(aMin, aMax, aAverage); + return aStat; +} + +void StatsAlerter::checkAndAlert(std::vector iInput) +{ + const float aMaxInputThreshold = *max_element(iInput.begin(), iInput.end()); + if (aMaxInputThreshold > aMaxThreshold) + { + //Input threshold is greater than Max Threshold. Set the Alerts + aAlerters[0]->emailSent = true; + aAlerters[1]->ledGlows = true; + } } From dcb9a8dd90baa577d00cd24509a45f15b135659a Mon Sep 17 00:00:00 2001 From: hrajpoot-js <65027195+hrajpoot-js@users.noreply.github.com> Date: Fri, 6 May 2022 14:42:27 +0530 Subject: [PATCH 3/3] Update stats-test.cpp --- stats-test.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stats-test.cpp b/stats-test.cpp index 8dd9303..4dc62a2 100644 --- a/stats-test.cpp +++ b/stats-test.cpp @@ -17,16 +17,19 @@ TEST_CASE("average is NaN for empty array") { auto computedStats = Statistics::ComputeStatistics({}); //All fields of computedStats (average, max, min) must be //NAN (not-a-number), as defined in math.h - + //Design the REQUIRE statement here. //Use http://www.cplusplus.com/reference/cmath/isnan/ + REQUIRE(std::isnan(computedStats.average)); + REQUIRE(std::isnan(computedStats.max)); + REQUIRE(std::isnan(computedStats.min)); } TEST_CASE("raises alerts when max is greater than threshold") { EmailAlert emailAlert; LEDAlert ledAlert; std::vector alerters = {&emailAlert, &ledAlert}; - + const float maxThreshold = 10.2; StatsAlerter statsAlerter(maxThreshold, alerters); statsAlerter.checkAndAlert({99.8, 34.2, 4.5, 6.7});