#include <stdio.h>
int Sum(int score[])
{int i,sum;
for(i=0,sum=0;i<10;i++)
{
sum+=score[i];
}
printf("成绩总分为%d\n",sum);
return sum;
}
int Max(int score[])
{
int i,max;
for(i=0;i<9;i++){
if(max<=score[i]){
max=score[i];
}
}
printf("成绩最高分为%d\n",max);
return max;
}
int Min(int score[])
{
int i,min;
for(i=0;i<9;i++){
if(min>=score[i]){
min=score[i];
}
}
printf("成绩最低分为%d\n",min);
return min;
}
int Avg(int score[],int n)
{int i,sum,avg;
for(i=0;i<10;i++)
{
sum+=score[i];
} avg=sum/n;
printf("成绩平均分为%d\n",avg);
return avg;
}
int Order(int score[])
{
int i,j,temp;
if(i!=9){printf("%d\n,",score[i]);}
else{printf("%d\n",score);}
for(i=8;i>=0;i--){
for(j=0;j<=i;j++)
if(score[j]<=score[j+1]){
temp=score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}return 0;
}
int main()
{
int score[]={67,98,75,63,82,79,81,91,66,84};
Sum(score);
Max(score);
Min(score);
Avg(score,10);
Order(score);
int i;
for(i=0;i<10;i++){
if(i!=9){
printf("%d\n,",score[i]);
}else{
printf("%d\n",score[i]);
}
}
return 0;
}
min,sum都需要初始化,min,max可以为数组第一个元素,sum为0
#include <stdio.h> int Sum ( int score[] ) { int i, sum = 0; for ( i=0; i<10; i++ ) { sum += score[i]; } printf ( "成绩总分为%d\n", sum ); return sum; } int Max ( int score[] ) { int i, max = 0; for ( i=0; i<10; i++ ) { if ( score[i] > max ) { max = score[i]; } } printf("成绩最高分为%d\n", max ); return max; } int Min ( int score[] ) { int i, min; for ( i=0; i<10; i++ ) { if ( min > score[i] ) { min = score[i]; } } printf ("成绩最低分为%d\n", min ); return min; } int Avg( int score[], int n ) { int i, sum, avg; for ( i=0; i<10; i++ ) { sum += score[i]; } avg = sum/n; printf ( "成绩平均分为%d\n", avg ); return avg; } void Order ( int score[] ) { int i,j,temp; for ( i = 8; i>= 0; i-- ) { for ( j=0; j<=i; j++) { if (score[j] < score[j+1] ) { temp = score[j]; score[j] = score[j+1]; score[j+1] = temp; } } } for ( i=0; i<10; i++) { printf("%d ",score[i]); } printf ( "\n" ); } int main() { int score[]={ 67, 98, 75, 63, 82, 79, 81, 91, 66, 84}; Sum( score ); Max( score ); Min( score ); Avg( score,10 ); Order( score ); return 0; }
max 没有初始化, 应为 int max = 0; 或者 int max = -1;