-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDay 0-finding mean,meadian,mode
65 lines (56 loc) · 1.04 KB
/
Day 0-finding mean,meadian,mode
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
float mean(int a[],int n)
{
float sum=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
}
return (sum/(float)n);
}
float median(int a[],int n)
{
if(n%2==0)
{
return(((float)a[n/2]+(float)a[(n/2)-1])/2.0);
}
else
return(float(a[(n/2)+1]));
}
int mode(int a[],int n)
{
int max=0,num;
for(int i=0;i<n;i++)
{ int count=0;
for(int j=0;j<n;j++)
{
if(a[i]==a[j])
count++;
}
if(max<count)
{
max=count;
num=a[i];
}
}
return num;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
float mn=mean(a,n);
float med=median(a,n);
int mo=mode(a,n);
printf("%.1f\n%.1f\n%d\n",mn,med,mo);
return 0;
}