-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
52 lines (45 loc) · 1.08 KB
/
solution.cpp
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
float arr[n];
float sum = 0, mean, mode, median;
for(int i=0 ; i<n ; i++) {
cin>>arr[i];
sum+=arr[i];
}
sort(arr, arr+n);
// now let us calculate the median
if(n%2 == 0) {
median = (arr[n/2] + arr[n/2-1])/2;
} else {
median = arr[n/2];
}
// calculating the mean
mean = sum/n;
//calculating the mode
mode = arr[0];
long mode_count = 1;
long temp = arr[0], temp_count=1;
for(int i=1 ; i<n ; i++) {
if(arr[i] != temp) {
if(temp_count > mode_count) {
mode = temp;
mode_count = temp_count;
}
temp = arr[i];
temp_count = 1;
} else {
temp_count++;
}
}
// now print the results
cout<<mean<<endl<<median<<endl<<mode;
return 0;
}