有12个学生,每个学生的数据包括学号、姓名、三门课程的成绩及其平均分。首先输入这12个学生的数据,然后再计算每个学生3门课程的平均分,并输出第6号学生的所有信息。
时间: 2023-12-15 11:59:03 浏览: 27
这道题可以使用结构体来存储每个学生的数据,然后通过循环计算每个学生的平均分,并输出第6号学生的所有信息。下面是一个示例代码:
```c
#include <stdio.h>
struct student {
int id;
char name[20];
float score1;
float score2;
float score3;
float avg_score;
};
int main() {
struct student stu[12];
int i;
// 输入每个学生的数据
for (i = 0; i < 12; i++) {
printf("请输入第%d个学生的学号、姓名、三门课程的成绩:\n", i+1);
scanf("%d %s %f %f %f", &stu[i].id, stu[i].name, &stu[i].score1, &stu[i].score2, &stu[i].score3);
}
// 计算每个学生的平均分
for (i = 0; i < 12; i++) {
stu[i].avg_score = (stu[i].score1 + stu[i].score2 + stu[i].score3) / 3;
}
// 输出第6号学生的所有信息
printf("第6号学生的信息为:\n");
printf("学号:%d\n", stu[5].id);
printf("姓名:%s\n", stu[5].name);
printf("第一门课程成绩:%f\n", stu[5].score1);
printf("第二门课程成绩:%f\n", stu[5].score2);
printf("第三门课程成绩:%f\n", stu[5].score3);
printf("平均分:%f\n", stu[5].avg_score);
return 0;
}
```
注意,数组下标是从0开始的,所以第6号学生的下标是5。在输出平均分时,需要在结构体中定义一个成员变量来存储平均分。