輸出成績降序排列時(shí)有亂碼。
#include?<stdio.h> //計(jì)算考試總分 int?total(int?score[]) { ????int?i; ????int?total?=?0; ????for(i=0;i<=9;i++) ????{ ????????total?+=?score[i]; ????} ????return?total; } //計(jì)算考試最高分?jǐn)?shù) int?highest(int?score[]) { ????int?i; ????for(i=0;i<=9;i++) ????{ ????????if(score[i]>score[i+1])?//調(diào)換順序 ????????{ ????????????int?n?=?score[i]; ????????????score[i]?=?score[i+1]; ????????????score[i+1]?=?n; ????????} ????} ????return?score[9]; } //計(jì)算考試最低分?jǐn)?shù) int?lowest(int?score[]) { ????int?i; ????for(i=0;i<=8;i++) ????{ ????????if(score[i]<score[i+1]) ????????{ ????????????int?n?=?score[i]; ????????????score[i]?=?score[i+1]; ????????????score[i+1]?=?n; ????????} ????} ????return?score[0]; } //計(jì)算考試平均分?jǐn)?shù) int?average(int?score[]) { ????int?n?=?total(score); ????int?av?=?n/10; ????return?av; } //考試分?jǐn)?shù)降序排列 int?down_ordinary(int?score[]) { ????int?i; ????int?j; ????for(i=9;i>=1;i--) ????{ ????????for(j=1;j<=i;j++) ????????{ ????????????if(score[j]<score[j+1]) ????????????{ ????????????????int?n?=?score[j]; ????????????????score[j]?=?score[j+1]; ????????????????score[j+1]?=?n; ????????????} ????????} ????} ????printf("考試成績降序排列為:"); ????for(i=1;i<=9;i++) ????{ ????????printf("%d?",score[i]); ????} ????return?0; } //主函數(shù) int?main() { ????int?score[10]={67,98,75,63,82,79,81,91,66,84}; ????printf("考試總分為%d\n",total(score)); ????printf("考試最高分為%d\n",highest(score)); ????printf("考試最低分為%d\n",lowest(score)); ????printf("考試平均分為%d\n",average(score)); ????down_ordinary(score); ????return?0; ???? ????}
輸出結(jié)果
考試總分為786 考試最高分為98 考試最低分為75 考試平均分為78 考試成績降序排列為:586547409?98?91?84?82?81?79?67?66
為什么多了一個(gè)586547409出來?還有 75,63兩項(xiàng)不見了
2017-03-05
#include <stdio.h>
//計(jì)算考試總分
int total(int score[])
{
??? int i;
??? int total = 0;
??? for(i=0;i<=9;i++)
??? {
??????? total += score[i];
??? }
??? return total;
}
?
?
//計(jì)算考試最高分?jǐn)?shù)
int highest(int score[])
{
??? int i;
??? for(i=0;i<=9;i++)
??? {
??????? if(score[i]>score[i+1]) //調(diào)換順序
??????? {
?????? ??? ?int n;
??????????? n = score[i];
??????????? score[i] = score[i+1];
??????????? score[i+1] = n;
??????? }
??? }
//??? printf("%d\n",score[9]);
??? return score[9];
}
?
//計(jì)算考試最低分?jǐn)?shù)
int lowest(int score[])
{
??? int i;
??? for(i=0;i<=8;i++)
??? {
??????? if(score[i]<score[i+1])
??????? {
??????????? int n = score[i];
??????????? score[i] = score[i+1];
??????????? score[i+1] = n;
??????? }
??? }
??? return score[0];
}
?
//計(jì)算考試平均分?jǐn)?shù)
int average(int score[])
{
??? int n = total(score);
??? int av = n/10;
??? return av;
}
?
?
//考試分?jǐn)?shù)降序排列
int down_ordinary(int score[])
{
??? int i;
??? int j;
??? for(i=9;i>=0;i--)
??? {
??????? for(j=0;j<i;j++)
??????? {
??????????? if(score[j]<score[j+1])
??????????? {
?????????? ??? ?int n;
??????????????? n = score[j];
??????????????? score[j] = score[j+1];
??????????????? score[j+1] = n;
??????????? }
??????? }
??? }
??? printf("考試分?jǐn)?shù)降序排列:");
??? for(i=0;i<10;i++)
??? {
//?? ??? ?printf("%d\n",score[0]);
??????? printf("%d ",score[i]);
??? }
??? return 0;
}
?
?
//主函數(shù)
int main()
{
??? int score[10]={67,98,75,63,82,79,81,91,66,84};
??? printf("考試總分為%d\n",total(score));
??? printf("考試最高分為%d\n",highest(score));
??? printf("考試最低分為%d\n",lowest(score));
??? printf("考試平均分為%d\n",average(score));
??? down_ordinary(score);
?
??? return 0;
??? ?
??? }
給你改了下,在“考試分?jǐn)?shù)降序排列”這下面的部分代碼錯(cuò)誤了,你再看看。
2017-01-22
把62-68行代碼改成:
for(i=9;i>=0;i--)
????{
????????for(j=0;j<i;j++)
????????{
????????????if(score[j]<score[j+1])
????????????{
? ? ? ? ? ? ? ? n?=?score[j];
就可以了。