#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 40
void sort(int a[], int n)
{
int i, j, k;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[k]) k = j;
}
if (k != i)
{
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
void init(int feedback[])
{
int i;
srand(time(NULL));
for (i = 0; i < N; i++)
{
feedback[i] = rand() % 10 + 1;
}
}
void print(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%4d", a[i]);
if ((i + 1) % 10 == 0) printf("\n");
}
}
float Average(int feedback[],int n)//求平均数
{
sort(feedback, n);
puts("");
print(feedback, n);
float sum = 0;
int i;
for (i = 0; i < n; i++)
{
sum += feedback[i];
}
return sum / n;
}
float Median(int feedback[],int n)//求中位数
{
float mean;
if (n % 2 == 0)
{
mean = (feedback[n / 2] + feedback[n / 2 - 1]) / 2.0;
}
else mean = feedback[n / 2];
return mean;
}
int Mod(int feedback[], int n)//求众数
{
int count[11] = { 0 };
int i;
for (i = 0; i < n; i++)
{
count[feedback[i]]++;
}
int max = count[1];
int pmax = 1;
for (i = 2; i <= 10; i++)
{
if (count[i] > max)
{
pmax = i;
max = count[i];
}
}
return pmax;
}
int main()
{
int feedback[N];
init(feedback);
int n = N;
print(feedback, n);
float aver = Average(feedback,n);
float median = Median(feedback,n);
int mod = Mod(feedback, n);
printf("aver=%f\nmedian=%f\nmod=%d", aver, median, mod);
return 0;
}
12-08
4959
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)