各位大神幫幫忙!哪里錯(cuò)了?
在一個(gè)長(zhǎng)度為10的整型數(shù)組里面,保存了班級(jí)10個(gè)學(xué)生的考試成績(jī)。要求編寫5個(gè)函數(shù),分別實(shí)現(xiàn)計(jì)算考試的總分,最高分,最低分,平均分和考試成績(jī)降序排序。
#include <stdio.h>
//計(jì)算總分
int getSumScore(int score[])
{
? ? int i;
? ? int sum=0;
? ? for(i=0;i<10;i++)
? ? {
? ? ? sum+=score[i];?
? ? }
? ? printf("平均分為%d\n",sum);
? ? return sum;
}
//計(jì)算平均分
int getAverage()
{
? ? int average;
? ? average=sum/10;
? ? printf("平均分為%d\n",average);
? ? return average;
}
//計(jì)算最高分
int maxScore(int score[]
{
? ? int max=-1;
? ? int j;
? ? for(j=0;j<10;j++)
? ? {
? ? ? ? if(score[j]>max)
? ? ? ? {
? ? ? ? ? ? max=score[j];
? ? ? ? ? ? printf("最高分為%d\n",max);
? ? ? ? }
? ? }
? ? return max;
}
//計(jì)算最低分
int minScore(int score[])
{
? ? int min=100;
? ? int k;
? ? for(k=0;k<10;k++)
? ? {
? ? ? ? if(score[k]<min)
? ? ? ? {
? ? ? ? ? ? min=score[k];
? ? ? ? ? ? printf("最低分為%d\n",min);
? ? ? ? }
? ? }
? ? return min;
}
//考試成績(jī)降序排序
void sort(int score[])
{
? ? int x,y;
? ? for(x=8; x>=0; i--)//一共10個(gè)數(shù),需要比較9次,0~8是9個(gè)數(shù)
? ? {
? ? ? ? for(y=0;y<=x;y++)
? ? ? ? {
? ? ? ? ? ? if(score[y]>score[y+1] ) ? ? //當(dāng)前面的數(shù)比后面的數(shù)大時(shí)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp; ? ? ? ? ?//定義臨時(shí)變量temp
? ? ? ? ? ? ? ? temp=score[y]; ? ? ?//將前面的數(shù)賦值給temp
? ? ? ? ? ? ? ? score[y]=score[y+1]; //前后之?dāng)?shù)顛倒位置
? ? ? ? ? ? ? ? score[y+1]=temp; ?//將較大的數(shù)放在后面 ? ?
? ? ? ? ? ? } ? ? ? ? ? ? ? ??
? ? ? ? }
? ? }
? ? printf("降序排序?yàn)?d\n",score[y]);
}
int main()
{
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ? getSumScore();
? ? getAverage();
? ? maxScore();
? ? minScore();
? ? sort();
? ? return 0;
}
編譯能通過(guò),但是結(jié)果好像有點(diǎn)問(wèn)題,能幫忙改一下嗎?謝謝?。。。?!
2017-02-11
是這個(gè)編譯能通過(guò),我搞錯(cuò)了,不好意思
2017-02-11
#include <stdio.h>
//計(jì)算總分和平均分?
int getSumScore(int score[])
{
? ? int i;
? ? int average;//平均分?
? ? int sum=0;
? ? for(i=0;i<10;i++)
? ? {
? ? ? sum+=score[i];?
? ? }
? ? printf("總分為%d\n",sum);
? ? average=sum/10;
? ? printf("平均分為%d\n",average);
? ? return average;
}
//計(jì)算最高分
int maxScore(int score[])
{
? ? int max=-1;
? ? int j;
? ? for(j=0;j<10;j++)
? ? {
? ? ? ? if(score[j]>max)
? ? ? ? {
? ? ? ? ? ? max=score[j];
? ? ? ? ? ? printf("最高分為%d\n",max);
? ? ? ? }
? ? }
? ? return max;
}
//計(jì)算最低分
int minScore(int score[])
{
? ? int min=100;
? ? int k;
? ? for(k=0;k<10;k++)
? ? {
? ? ? ? if(score[k]<min)
? ? ? ? {
? ? ? ? ? ? min=score[k];
? ? ? ? ? ? printf("最低分為%d\n",min);
? ? ? ? }
? ? }
? ? return min;
}
//考試成績(jī)降序排序
void sort(int score[])
{
? ? int x,y;
? ? for(x=8; x>=0; x--)//一共10個(gè)數(shù),需要比較9次,0~8是9個(gè)數(shù)
? ? {
? ? ? ? for(y=0;y<=x;y++)
? ? ? ? {
? ? ? ? ? ? if(score[y]>score[y+1] ) ? ? //當(dāng)前面的數(shù)比后面的數(shù)大時(shí)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp; ? ? ? ? ?//定義臨時(shí)變量temp
? ? ? ? ? ? ? ? temp=score[y]; ? ? ?//將前面的數(shù)賦值給temp
? ? ? ? ? ? ? ? score[y]=score[y+1]; //前后之?dāng)?shù)顛倒位置
? ? ? ? ? ? ? ? score[y+1]=temp; ?//將較大的數(shù)放在后面 ? ?
? ? ? ? ? ? } ? ? ? ? ? ? ? ??
? ? ? ? }
? ? }
? ? printf("降序排序?yàn)?d\n",score[y]);
}
int main()
{
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ? getSumScore(score);
? ? getAverage(score);
? ? maxScore(score);
? ? minScore(score);
? ? sort(score);
? ? return 0;
}
2017-02-10