-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature-tracker.cpp
More file actions
160 lines (128 loc) · 4.11 KB
/
temperature-tracker.cpp
File metadata and controls
160 lines (128 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/** @file temperature-tracker.cpp
* @brief
* You decide to test if your oddly-mathematical heating company is fulfilling its All-Time Max, Min, Mean and Mode Temperature Guarantee™.
*
* Write a class TempTracker with these methods:
*
* insert()—records a new temperature
* get_max()—returns the highest temp we've seen so far
* get_min()—returns the lowest temp we've seen so far
* get_mean()—returns the mean ↴ of all temps we've seen so far
* get_mode()—returns a mode ↴ of all temps we've seen so far
*
* Optimize for space and time. Favor speeding up the getter functions (get_max(), get_min(), get_mean(), and get_mode()) over speeding up the insert() function.
*
* get_mean() should return a float, but the rest of the getter functions can return integers. Temperatures will all be inserted as integers.
* We'll record our temperatures in Fahrenheit, so we can assume they'll all be in the range 0..110.
*
* If there is more than one mode, return any of the modes.
*
* @author Shawn Mullings (@shawnmullings)
* @bug No known bugs.
*/
/* -- Includes -- */
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <string>
#include <map>
class TempTracker{
public:
/* Contructor */
TempTracker(){
this->_max_temp = 0;
this->_min_temp = 110;
}
/* Destructor */
~TempTracker(){}
/* Add temperature to container method and initiate max, min, mean, and mode calculations */
void insert(int value){
//if (value == NULL)
// return;
this->_temps.push_back(value);
get_max_helper(value);
get_min_helper(value);
get_mean_helper();
get_mode_helper();
}
/* Calculate maximum temperature method */
int get_max(){ return this->_max_temp; }
/* Calculate minimum temperature method */
int get_min(){ return this->_min_temp; }
/* Calculate mean method */
float get_mean(){ return this->_mean_temp; }
/* Calculate mode method */
int get_mode(){ return this->_mode_temp; }
/* Output temperature to console */
void trace(){
/* use string stream to convert vector items to string for "cout" */
std::stringstream result;
std::copy(this->_temps.begin(), this->_temps.end(), std::ostream_iterator<int>(result, " "));
std::cout << result.str() << std::endl;
}
private:
std::vector<int> _temps; /* Temperature container */
int _max_temp; /* Maximum temperature variable */
int _min_temp; /* Minimum temperature variable */
float _mean_temp; /* Average temperature variable */
float _mode_temp; /* Most instances temperature variable */
/* get max helper method */
void get_max_helper(int value){
_max_temp = (value > _max_temp) ? value : _max_temp;
}
/* get min helper method */
void get_min_helper(int value){
_min_temp = (value < _min_temp) ? value : _min_temp;
}
/* get average (mean) helper method */
void get_mean_helper(){
int sum_of_elems = 0;
for (std::vector<int>::iterator it = _temps.begin(); it != _temps.end(); it++)
sum_of_elems += (*it);
_mean_temp = (float)(sum_of_elems/_temps.size());
}
/* get most instances (max) helper method */
void get_mode_helper(){
int max;
max = 0;
int most_common;
most_common = -1;
std::map<int,int> m;
for (std::vector<int>::iterator vi = this->_temps.begin(); vi != this->_temps.end(); vi++) {
m[*vi]++;
if (m[*vi] > max) {
max = m[*vi];
this->_mode_temp = *vi;
}
}
}
};
int main(int argc, char const *argv[])
{
/* Initailize TempTracker object */
TempTracker tt;
/* Add some temperatures */
tt.insert(45);
tt.insert(50);
tt.insert(74);
tt.insert(82);
tt.insert(98);
tt.insert(32);
tt.insert(17);
tt.insert(101);
tt.insert(101);
tt.insert(105);
tt.insert(63);
tt.insert(98);
tt.insert(45);
tt.insert(45);
tt.trace();
/* Output max, min, mean, and mode to console */
std::cout << "Max: " << tt.get_max() << std::endl;
std::cout << "Min: " << tt.get_min() << std::endl;
std::cout << "Mean: " << tt.get_mean() << std::endl;
std::cout << "Mode: " << tt.get_mode() << std::endl;
return 0;
}