3个回答
展开全部
#include <stdio.h>
int calculate_mode(int number [],int n)//求众数
{
int i,temp;
int p_count=1,count=1;
temp=number[0];
for(i=1;i<n;i++)
if(temp==number[i])
{
count++;
temp=number[i];
}
else
{
if(count>p_count)
{
p_count=count;//更新众数
}
count=1;
temp=number[i];
}
return p_count;
}
double calculate_mean(int number [],int n)//求平均值
{
int i;
double sum=0;
for(i=0;i<n;i++)
{
sum+=number[i];
}
return sum/n;
}
double calculate_median(int number [],int n)//求中位数
{
if(n%2==0)
return (number[n/2-1]+number[n/2])/2.0;
else
return number[n/2];
}
int main(void)
{
int n,i,j,number[100]={0},temp;
double mean,median;
int mode;
printf("请问需要输入几位整数?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&number[i]);
for(i=0;i<n;i++)//排序,方便计算
for(j=i+1;j<n;j++)
if(number[i]>number[j])
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
mean=calculate_mean(number,n);
median=calculate_median(number,n);
mode=calculate_mode(number,n);
printf("平均值是%f\n中位数是%f\n众数是%d",mean,median,mode);
return 0;
}
刚写的~~~如果有错误告诉我
追问
能把他以文字的形式发过来么
展开全部
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *a = (int *)malloc(sizeof(int) * 100);
int i = 0, j = 100;
puts("输入x结束:");
scanf("%d", &a[i]);
do
{
i++;
if(i == j)
{
j = j + 100;
a = (int *)realloc(a, j * sizeof(int));
}
}while(scanf("%d", &a[i]) && a[i] != 'x');
int k, l = 0;
for(k = 0; k < i; k++)
{
l = l + a[k];
}
printf("平均数是:\n%d\n", l / i);
int t;
for(k = 0; k < i; k++)
{
for(l = 0; l < i; l++)
{
if(a[l] > a[l + 1])
{
t = a[l];
a[l] = a[l + 1];
a[l + 1] = t;
}
}
}
l = a[i - 1] + 1;
j = 0;
int m = 1;
int n = 0;
int b[i];
b[0] = l;
int v = 0;
int c = 0;
while(j < i)
{
while(a[j] == a[j - 1])
{
m++;
l = a[j];
j++;
}
if(m > n)
{
n = m;
b[0] = l;
c = 0;
v = 0;
}
else if(m == n)
{
v++;
b[v] = l;
c = 1;
}
m = 1;
j++;
}
if(c == 0)
printf("众数是%d", b[0]);
else
{
puts("众数分别是:");
for(k = 0; k <= v; k++)
{
printf("%d,", b[k]);
}
}
if(i % 2 == 0)
printf("中位数是:\n%d\n", a[(i / 2 + (i + 2) / 2 ) / 2]);
else
printf("中位数是:\n%d\n", a[(i + 1) / 2]);
system("pause");
}
#include <stdlib.h>
int main(void)
{
int *a = (int *)malloc(sizeof(int) * 100);
int i = 0, j = 100;
puts("输入x结束:");
scanf("%d", &a[i]);
do
{
i++;
if(i == j)
{
j = j + 100;
a = (int *)realloc(a, j * sizeof(int));
}
}while(scanf("%d", &a[i]) && a[i] != 'x');
int k, l = 0;
for(k = 0; k < i; k++)
{
l = l + a[k];
}
printf("平均数是:\n%d\n", l / i);
int t;
for(k = 0; k < i; k++)
{
for(l = 0; l < i; l++)
{
if(a[l] > a[l + 1])
{
t = a[l];
a[l] = a[l + 1];
a[l + 1] = t;
}
}
}
l = a[i - 1] + 1;
j = 0;
int m = 1;
int n = 0;
int b[i];
b[0] = l;
int v = 0;
int c = 0;
while(j < i)
{
while(a[j] == a[j - 1])
{
m++;
l = a[j];
j++;
}
if(m > n)
{
n = m;
b[0] = l;
c = 0;
v = 0;
}
else if(m == n)
{
v++;
b[v] = l;
c = 1;
}
m = 1;
j++;
}
if(c == 0)
printf("众数是%d", b[0]);
else
{
puts("众数分别是:");
for(k = 0; k <= v; k++)
{
printf("%d,", b[k]);
}
}
if(i % 2 == 0)
printf("中位数是:\n%d\n", a[(i / 2 + (i + 2) / 2 ) / 2]);
else
printf("中位数是:\n%d\n", a[(i + 1) / 2]);
system("pause");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询