為什么算不出結果?
#include <stdio.h>
int main()
{
??? int score[]={67,98,75,63,82,79,81,91,66,84};
??? int sum;
??? int max=score[0];
??? int min=score[0];
??? int i,j;
??? for(i=0;i<10;i++)
??? {
??????? sum+=score[i];
??????? if(score[i]>=max)
??????? {
??????????? max=score[i];
??????? }
??????? else if(score[i]<min)
??????? {
??????????? min=score[i];
??????? }
??? }
??? printf("計算考試的總分為:%d\n",sum);
??? printf("計算考試的最高分為:%d\n",max);
??? printf("計算考試的最低分為:%d\n",min);
??? double averge=sum/10.0;
??? printf("計算考試的平均分為:%.1f\n",averge);
??? for(i=8;i>=0;i--)
??? {
??????? for(j=0;j<=i;j)
??????? {
??????????? if(score[j]<score[j+1])
??????????? {
??????????????? int temp;
??????????????? temp=score[j+1];
??????????????? score[j+1]=score[j];
??????????????? score[j]=temp;
??????????? }
??????? }
??? }
??? printf("計算考試的降序排序為:");
??? for(i=0;i<10;i++)
??? {
??????? if(i==9)
??????? {
??????????? printf("%d",score[i]);
??????? }
??????? else
??????? {
??????????? printf("%d, ",score[i]);
??????? }
??? }
??? return 0;
}
2018-10-10
第三個for語句:???
for(i=8;i>=0;i--)
??? {
??????? for(j=0;j<=i;j); 這里你沒有j++。你的程序也就沒有完成降序而已,就是這里的細節出了問題。你試試改這里就好了,我已經驗證成功了。
???
?
2018-10-10
#include <stdio.h>
int main()
{
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ??
? ??
? ? int sum(int score[])
? ? {
? ? ? ? int sum;
? ? ? ? for(int i=0;i<10;i++)
? ? ? ? {
? ? ? ? ? ? sum+=score[i];
? ? ? ? ? ??
? ? ? ? }
? ? ? ??
? ? ? ? return sum;
? ? }
? ??
? ? int topScore(int score[])
? ? {
? ? ? ? int top = 0;
? ? ? ? for(int i=0;i<10;i++)
? ? ? ? {
? ? ? ? ? ? if(score[i]>top)
? ? ? ? ? ? top = score[i];
? ? ? ? }
? ? ? ? return top;
? ? }
? ??
? ? int bottomScore(int score[])
? ? {
? ? ? ? int bottom = 100;
? ? ? ? for(int i=0;i<10;i++)
? ? ? ? {
? ? ? ? ? ? if(score[i]<bottom)
? ? ? ? ? ? bottom = score[i];
? ? ? ? }
? ? ? ? return bottom;
? ? }
? ??
? ? int average(int score[])
? ? {
? ? ? ? int i;
? ? ? ? int sum = 0;
? ? ? ??
? ? ? ? for(i=0;i<10;i++)
? ? ? ? {
? ? ? ? ? ? sum += score[i];
? ? ? ? }
? ? ? ??
? ? ? ? return sum/i;
? ? }
? ??
? ? void bubble_sort(int arr[],int n)
? ? {
? ? ? ? int temp;
? ? ? ? for(int i=n-1; i>0; i--)
? ? ? ? {
? ? ? ? ? ? for(int j=n-1; j>0; j--)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(arr[j] > arr[j-1])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? temp = arr[j];
? ? ? ? ? ? ? ? ? ? arr[j] = arr[j-1];
? ? ? ? ? ? ? ? ? ? arr[j-1] = temp;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ??
? ? printf("sum=%d\n",sum(score));
? ? printf("topScore=%d\n",topScore(score));
? ? printf("bottomScore=%d\n",bottomScore(score));
? ? printf("averageScore=%d\n",average(score));
? ??
? ? printf("***********排序后********\n");
? ??
? ? bubble_sort(score,10);
? ??
? ? for(int i=0; i<10; i++)
? ? {
? ? ? ? printf("%d ",score[i]);
? ? }
? ??
? ? return 0;
? ??
}