Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions stats-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAlerter*> alerters = {&emailAlert, &ledAlert};

const float maxThreshold = 10.2;
StatsAlerter statsAlerter(maxThreshold, alerters);
statsAlerter.checkAndAlert({99.8, 34.2, 4.5, 6.7});
Expand Down
42 changes: 40 additions & 2 deletions stats.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
#include "stats.h"

Stats Statistics::ComputeStatistics(const std::vector<___>& ) {
//Implement statistics here
Stats Statistics::ComputeStatistics(const std::vector<double>& 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<float> 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;
}
}
45 changes: 43 additions & 2 deletions stats.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
#include <vector>
#include <bits/stdc++.h>

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<double>& 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<IAlerter*> aAlerters;

public:
StatsAlerter(const float iMaxThreshold, std::vector<IAlerter*> iAlerters):
aMaxThreshold(iMaxThreshold), aAlerters(iAlerters) { }

void checkAndAlert(std::vector<float> iInput);
};