-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday0_1
66 lines (61 loc) · 1.66 KB
/
day0_1
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
// In this challenge, we practice calculating the mean, median, and mode. //
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
float meanValue(std::vector<int> v) {
float sum = 0;
for(long unsigned i = 0; i < v.size(); i++){
sum += v[i];
}
return (sum / v.size());
}
float medianValue(std::vector<int> v) {
float median = 0;
std::sort(v.begin(), v.end());
if(v.size() % 2 == 0){
int middleHigher = (v.size() / 2);
int middleLower = middleHigher - 1;
float sum = v[middleLower] + v[middleHigher];
return sum / 2;
} else {
median = v[v.size()/2];
return median;
}
return -1;
}
float modeValue(std::vector<int> v){
map<int, int> modeMap;
for(long unsigned i = 0; i < v.size(); i++){
modeMap[v[i]]++;
}
int currMaxValue, currMaxAmount = 0;
std::sort(v.begin(), v.end());
for(auto it = modeMap.begin(); it != modeMap.end(); ++it){
if(it -> first == v[0]) {
currMaxValue = it -> first;
}
if((it -> second) > currMaxAmount && (it -> first >= v[0])) {
currMaxAmount = it -> second;
currMaxValue = it -> first;
}
}
return currMaxValue;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int size, value;
cin >> size;
vector<int> v;
while(cin >> value){
v.push_back(value);
}
float mean = meanValue(v);
float median = medianValue(v);
int mode = modeValue(v);
cout << mean << endl << median << endl << mode << endl;
return 0;
}