Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------Mean.h-----------------------------
- // Copyright 2019 Ian McDowell
- #ifndef _CSCE240_HW_HW6_INC_MEAN_H //NOLINT
- #define _CSCE240_HW_HW6_INC_MEAN_H //NOLINT
- /* This class inherits from the Statistic class such that it may be used
- * polymorphically.
- */
- #include <statistic.h>
- #include <list>
- namespace csce240 {
- class Mean : public std::list<double>, public Statistic {
- public:
- /* Stores data (datum) such than an average may be calculated.
- * - NOTE: You do not necessarily need to store each datum.
- */
- void Collect(double datum);
- /* Returns the mean of the data (datum) from the Collect method.
- */
- double Calculate() const;
- };
- } // namespace csce240
- #endif //NOLINT
- ----------------------Mean.cc-----------------------------
- // Copyright 2019 Ian McDowell
- #include <mean.h>
- using std::list;
- namespace csce240 {
- void Mean::Collect(double datum) {
- push_front(datum);
- }
- double Mean::Calculate() const {
- list<double> tempList;
- int i = 0;
- double temp;
- double avg = 0;
- while (!empty()) {
- temp = back();
- avg += temp;
- tempList.push_front(temp);
- pop_back();
- ++i;
- }
- avg = avg / i;
- while(!tempList.empty()) {
- push_front(tempList.back());
- tempList.pop_back();
- }
- return avg;
- }
- } // namespace csce240
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement