一維數組編譯
Ⅰ c語言一維數組
#include "stdio.h"
#define N 30
void main()
{
int grades[N]; //所有人的分數
int upAverage[N]; //超過平均分的學生序號
int unPass[N]; //不及格的學生序號
int nNum = 0; //學生總人數
int nUpAve = 0; //超過平均分的人數
int totGrade = 0; //總分
int n50,n60,n70,n80,n90; //各個分數段人數
double average; //平均分
int i;
//讀入成績
while (1){
scanf("%d",&grades[nNum]);
if (grades[nNum]<0) break;
totGrade += grades[nNum];
nNum++;
}
average = totGrade*1.0/nNum;
//統計各個分數段人數
n50 = n60 = n70 = n80 = n90 = 0;
for (i=0; i if (grades[i] >= average) upAverage[nUpAve++] = i;
if (grades[i] < 60) unPass[n50++] = i;
else if (grades[i] < 70) n60++;
else if (grades[i] < 80) n70++;
else if (grades[i] < 90) n80++;
else n90++;
}
//結果輸出
printf("不及格人數: %d\n",n50);
printf("不及格學生序號:");
for (i=0; i printf("\n成績在全班平均分及平均分之上的學生人數: %d\n",nUpAve);
printf("這些學生序號:");
for (i=0; i printf("\n以直方圖方式統計各分數段的學生人數及所佔的百分比\n");
printf("\n分數段 \t 學生人數\t圖形");
printf("\n <60 \t %2d(%.0lf%%) \t", n50, n50*100.0/nNum); for (i=0; i printf("\n 60-69 \t %2d(%.0lf%%) \t", n60, n60*100.0/nNum); for (i=0; i printf("\n 70-79 \t %2d(%.0lf%%) \t", n70, n70*100.0/nNum); for (i=0; i printf("\n 80-89 \t %2d(%.0lf%%) \t", n80, n80*100.0/nNum); for (i=0; i printf("\n >=90 \t %2d(%.0lf%%) \t", n90, n90*100.0/nNum); for (i=0; i printf("\n");
}