用c语言写出一下内容:有5个学生,每个学生的数据包括学号、姓名,4门课程二点成绩,从键盘输入5个学生的数据,要求输出5个学生4门课程总成绩以及每门课程的平均分,以及每个学生未达到平均分的课程数和每门课没有达到平均分的人数
时间: 2023-10-28 21:32:38 浏览: 54
好的,以下是用C语言实现的代码:
```c
#include <stdio.h>
struct Student {
int id;
char name[20];
int scores[4];
int total_score;
float avg_score;
int below_avg_count;
};
int main() {
struct Student students[5];
int i, j, sum;
for (i = 0; i < 5; i++) {
printf("请输入学生%d的学号:", i+1);
scanf("%d", &students[i].id);
printf("请输入学生%d的姓名:", i+1);
scanf("%s", students[i].name);
printf("请输入学生%d的4门课程成绩:\n", i+1);
sum = 0;
for (j = 0; j < 4; j++) {
scanf("%d", &students[i].scores[j]);
sum += students[i].scores[j];
}
students[i].total_score = sum;
students[i].avg_score = (float)sum / 4;
}
// 计算每门课程的平均分
float course_avg[4];
for (j = 0; j < 4; j++) {
sum = 0;
for (i = 0; i < 5; i++) {
sum += students[i].scores[j];
}
course_avg[j] = (float)sum / 5;
}
// 统计每个学生未达到平均分的课程数和每门课没有达到平均分的人数
int below_avg_count[5] = {0};
int below_course_count[4] = {0};
for (i = 0; i < 5; i++) {
for (j = 0; j < 4; j++) {
if (students[i].scores[j] < course_avg[j]) {
below_avg_count[i]++;
below_course_count[j]++;
}
}
students[i].below_avg_count = below_avg_count[i];
}
// 输出结果
printf("\n学号\t姓名\t课程1\t课程2\t课程3\t课程4\t总成绩\t平均分\t未达平均分课程数\n");
for (i = 0; i < 5; i++) {
printf("%d\t%s\t", students[i].id, students[i].name);
for (j = 0; j < 4; j++) {
printf("%d\t", students[i].scores[j]);
}
printf("%d\t%.2f\t%d\n", students[i].total_score, students[i].avg_score, students[i].below_avg_count);
}
printf("\n课程1平均分:%.2f\t未达平均分人数:%d\n", course_avg[0], below_course_count[0]);
printf("课程2平均分:%.2f\t未达平均分人数:%d\n", course_avg[1], below_course_count[1]);
printf("课程3平均分:%.2f\t未达平均分人数:%d\n", course_avg[2], below_course_count[2]);
printf("课程4平均分:%.2f\t未达平均分人数:%d\n", course_avg[3], below_course_count[3]);
return 0;
}
```
希望能对您有所帮助!
阅读全文