用c语言编写:输入五个学生的三门课程成绩,求每个学生的平均成绩和每门课程的平均成绩
时间: 2023-03-03 16:35:56 浏览: 342
以下是一个用C语言编写的程序,可以实现输入五个学生的三门课程成绩,求每个学生的平均成绩和每门课程的平均成绩。这里假设每门课程的成绩都是一个整数。
```c
#include <stdio.h>
int main() {
int scores[5][3];
int i, j;
int student_sum[5] = {0};
int course_sum[3] = {0};
// 输入每个学生的三门课程成绩
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的三门课程成绩:\n", i+1);
for (j = 0; j < 3; j++) {
scanf("%d", &scores[i][j]);
student_sum[i] += scores[i][j];
course_sum[j] += scores[i][j];
}
}
// 输出每个学生的平均成绩
printf("每个学生的平均成绩:\n");
for (i = 0; i < 5; i++) {
printf("第%d个学生的平均成绩为:%.2f\n", i+1, (float)student_sum[i]/3);
}
// 输出每门课程的平均成绩
printf("每门课程的平均成绩:\n");
for (j = 0; j < 3; j++) {
printf("第%d门课程的平均成绩为:%.2f\n", j+1, (float)course_sum[j]/5);
}
return 0;
}
```
该程序首先定义了一个二维数组`scores`来存储每个学生的三门课程成绩,以及两个一维数组`student_sum`和`course_sum`,分别用于存储每个学生的总成绩和每门课程的总成绩。程序使用嵌套的循环来依次输入每个学生的三门课程成绩,并且累加每个学生的总成绩和每门课程的总成绩。最后,程序根据累加的数据计算并输出每个学生的平均成绩和每门课程的平均成绩。