Advertisement
Guest User

stackoverflow question code

a guest
Nov 27th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. ----------------------Mean.h-----------------------------
  2. // Copyright 2019 Ian McDowell
  3. #ifndef _CSCE240_HW_HW6_INC_MEAN_H //NOLINT
  4. #define _CSCE240_HW_HW6_INC_MEAN_H //NOLINT
  5.  
  6. /* This class inherits from the Statistic class such that it may be used
  7.  * polymorphically.
  8.  */
  9. #include <statistic.h>
  10. #include <list>
  11.  
  12. namespace csce240 {
  13.  
  14. class Mean : public std::list<double>, public Statistic {
  15.  public:
  16.  
  17.   /* Stores data (datum) such than an average may be calculated.
  18.    * - NOTE: You do not necessarily need to store each datum.
  19.    */
  20.   void Collect(double datum);
  21.  
  22.   /* Returns the mean of the data (datum) from the Collect method.
  23.    */
  24.   double Calculate() const;
  25. };
  26.  
  27. } // namespace csce240
  28.  
  29. #endif //NOLINT
  30.  
  31.  
  32. ----------------------Mean.cc-----------------------------
  33. // Copyright 2019 Ian McDowell
  34. #include <mean.h>
  35. using std::list;
  36.  
  37. namespace csce240 {
  38.  
  39. void Mean::Collect(double datum) {
  40.     push_front(datum);
  41. }
  42.  
  43. double Mean::Calculate() const {
  44.     list<double> tempList;
  45.     int i = 0;
  46.     double temp;
  47.     double avg = 0;
  48.  
  49.     while (!empty()) {
  50.         temp = back();
  51.         avg += temp;
  52.         tempList.push_front(temp);
  53.         pop_back();
  54.         ++i;
  55.     }
  56.  
  57.     avg = avg / i;
  58.  
  59.     while(!tempList.empty()) {
  60.         push_front(tempList.back());
  61.         tempList.pop_back();
  62.     }
  63.  
  64.     return avg;
  65. }
  66.  
  67.  
  68. } // namespace csce240
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement