C语言:设有m个人的成绩存放在score数组中,请编写函数,将高于平均分的人数作为函数值返回......

2024-11-22 13:18:04
推荐回答(4个)
回答(1):

#include
int deal(int a[],int m,int up[]);

int main()
{
int i,j,count=0;
int score[]={1,2,3,4,5,6,7,8,9,0,10},up[100]={-1};
count=deal(score,11,up);
printf("%d\n",count);
for(i=0;i printf("%-4d",up[i]);
return 0;
}
int deal(int a[],int m,int up[])
{
double sum=0,aver;
int i=0,count=0,j=0;
while(i {
sum=sum+a[i];
i++;
}
aver=sum*1.0/m;
for(i=0;i {
if(a[i]>aver)
{
count++;
up[j]=a[i];
j++;
}
}
return count;
}
输出为:
5
6 7 8 9 10
但是数字要是大且多得话,就得考虑溢出了!

回答(2):

#include
int main()
{
int m,i,j=0,a[100],*up,score[100],sum=0;
double aver;
up=a;

printf("输入m\n");
scanf("%d",&m);

printf("输入成绩(为整数)\n");

for(i=0;i scanf("%d",&score[i]);

for(i=0;i sum=sum+score[i];

aver=(double)sum/m;
printf("%lf\n",aver);
printf("高于平均分的有\n");

for(i=0;i {
if(score[i]>aver)
{ a[j]=score[i];

printf("%d\n",a[j]);
j++;
}
}
return 0;
}

回答(3):

#include
int highaverage(float s[],float stu[],int x);

int main()
{
int m,n;
float score[20],up[20];

printf("input M:");
scanf("%d",&m);

for(int i=0;i {
printf("score[%d]=",i);
scanf("%f",&score[i]);
}

n=highaverage(score,up,m);

for(int j=0;j printf("%3.1f ",up[j]);

return 0;

}

int highaverage(float s[],float stu[],int x)
{
float average,sum;
int num=0;

for(int i=0;i {
sum+=s[i];
}

average=sum/x;

for(int j=0;j {
if(s[j]>average)
{stu[num]=s[j];num++;}
}

return num;
}

回答(4):

int upp[100];
int* up=upp;
int upaver(int s[],int* up,int m){
int i,cnt,aver,sum=0;
for(i=0;i sum+=s[i];
aver = sum/m;
for(i=0;i if(s[i]>aver){
*up++=s[i];
cnt++;
}
}
return cnt;
}
调用时,int n = upaver(score,up, m);