实验六:成绩统计器
1.定义5行3列实型二维数组,并用AVERAGE函数进行统计
2.采用二维循环从键盘给数组每个元素分别赋值,并加入数据检测机制(只接受介于0至100之间的浮点数据)
3.编写AVERAGE函数进行计算平均值
4.计算每个学生的平均分,并输出
5.计算每门课程的平均分,并输出
6.拥有简单的用户交互界面
代码:
1 #include2 #include 3 int main() 4 { 5 float AVERAGE(float a[], int n); 6 float grade[5][3]; 7 float student1[3],student2[3],student3[3],student4[3],student5[3]; 8 float subject1[5],subject2[5],subject3[5]; 9 float subav1, subav2, subav3;10 float stuav1, stuav2, stuav3, stuav4, stuav5;11 float k;12 int i, j;13 printf("==========================welcome to the system=============================\n");14 for (i = 1; i <= 5; i++)15 {16 for (j = 1; j <= 3; j++)17 {18 printf("===input the %dth subject grade of the %dth student===\n", j, i);19 aa:scanf_s("%f", &k);20 if (k <= 100 && k >= 0)21 {22 grade[i-1][j-1] = k;23 }24 else25 {26 printf("input the right grade!\n");27 goto aa;28 }29 }30 }31 system("CLS");32 printf("==============subject1========subject2==========subject3==\n");33 for (i = 1; i <= 5; i++)34 {35 printf("student%d %f %f %f\n", i, grade[i-1][0], grade[i-1][1], grade[i-1][2]);36 }37 printf("====================================================\n");38 for (i = 0; i < 5; i++)39 {40 subject1[i] = grade[i][0];41 subject2[i] = grade[i][1];42 subject3[i] = grade[i][2];43 }44 for (j = 0; j < 3; j++)45 {46 student1[j] = grade[0][j];47 student2[j] = grade[1][j];48 student3[j] = grade[2][j];49 student4[j] = grade[3][j];50 student5[j] = grade[4][j];51 }52 stuav1 = AVERAGE(student1, 3);53 stuav2 = AVERAGE(student2, 3);54 stuav3 = AVERAGE(student3, 3);55 stuav4 = AVERAGE(student4, 3);56 stuav5 = AVERAGE(student5, 3);57 subav1 = AVERAGE(subject1, 5);58 subav2 = AVERAGE(subject2, 5);59 subav3 = AVERAGE(subject3, 5);60 printf(" AVERAGE\n");61 printf("student1 %f\n", stuav1);62 printf("student2 %f\n", stuav2);63 printf("student3 %f\n", stuav3);64 printf("student4 %f\n", stuav4);65 printf("student5 %f\n", stuav5);66 printf("==================================\n");67 printf("subject1 %f\n", subav1);68 printf("subject2 %f\n", subav2);69 printf("subject3 %f\n", subav3);70 system("pause");71 return 0;72 }73 float AVERAGE(float a[],int n)74 {75 float sum=0;76 int i;77 for (i = 0; i < n; i++)78 {79 sum = sum + a[i];80 }81 return sum / n;82 }
演示:
改进方案:1.调整输出数据位数
2.增加对文件读写的支持
(ver2.0)