数组练习
在一个长度为10的整型数组里面,保存了班级10个学生的考试成绩。要求编写5个函数,分别实现计算考试的总分,最高分,最低分,平均分和考试成绩降序排序。
#include <stdio.h>
#include <string.h>
int totalScore(int score[])
{
int sum=0,i;
for(i=0;i<=9;i++)
{
sum += score[i];
}
return sum;
}
int maxScore(int score[])
{
int i,max=0;
for(i=0;i<=9;i++)
{
if(score[i]>max)
{max = score[i];}
}
return max;
}
int minScore(int score[])
{
int i,min=100;
for(i=0;i<=9;i++)
{
if(score[i]<min)
{min = score[i];}
}
return min;
}
float averageScore(int score[])
{
return totalScore(score)/10;
}
void sortScore(int score[])
{
int i,j,temp;
for(i=0;i<=8;i++)
{
for(j=0;j<=8;j++)
{
if(score[j]<score[j + 1])
{
temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
}
int main()
{
int i;
int score[10]={67,98,75,63,82,79,81,91,66,84};
printf("%d\n",totalScore(score));
printf("%d\n",maxScore(score));
printf("%d\n",minScore(score));
printf("%1.1f\n",averageScore(score));
sortScore(score);
for(i=0;i<=9;i++)
{
printf("%d ",score[i]);
}
return 0;
}