c语言有八个学生,每个学生包括学号,姓名,5门课的成绩,从键盘输入5个学生数据,要求输出5个学生4门课总成绩和每门课的平均分,以及每个学生未达到平均分的课程数和每门课没达到平均分的人数
时间: 2024-01-03 12:04:19 浏览: 153
好的,这是一道关于C语言的编程问题。根据您的要求,我可以给出下面的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define STUDENT_NUM 8
#define COURSE_NUM 5
typedef struct {
int id;
char name[20];
int scores[COURSE_NUM];
} Student;
void calc_total_and_avg(Student students[], int total_scores[], float avg_scores[]) {
for (int i = 0; i < COURSE_NUM; i++) {
total_scores[i] = 0;
for (int j = 0; j < STUDENT_NUM; j++) {
total_scores[i] += students[j].scores[i];
}
avg_scores[i] = (float)total_scores[i] / STUDENT_NUM;
}
}
void calc_fail_count(Student students[], int fail_count[]) {
for (int i = 0; i < COURSE_NUM; i++) {
fail_count[i] = 0;
for (int j = 0; j < STUDENT_NUM; j++) {
if (students[j].scores[i] < avg_scores[i]) {
fail_count[i]++;
}
}
}
}
void print_result(Student students[], int total_scores[], float avg_scores[], int fail_count[]) {
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t成绩4\t成绩5\t总成绩\t平均分\t未达标数\n");
for (int i = 0; i < STUDENT_NUM; i++) {
int total_score = 0;
for (int j = 0; j < COURSE_NUM; j++) {
total_score += students[i].scores[j];
}
float avg_score = (float)total_score / COURSE_NUM;
int fail_course_count = 0;
for (int j = 0; j < COURSE_NUM; j++) {
if (students[i].scores[j] < avg_scores[j]) {
fail_course_count++;
}
}
printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\t%d\n", students[i].id, students[i].name,
students[i].scores[0], students[i].scores[1], students[i].scores[2], students[i].scores[3], students[i].scores[4],
total_score, avg_score, fail_course_count);
}
printf("平均分\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", avg_scores[0], avg_scores[1], avg_scores[2], avg_scores[3], avg_scores[4]);
printf("未达标数\t%d\t%d\t%d\t%d\t%d\n", fail_count[0], fail_count[1], fail_count[2], fail_count[3], fail_count[4]);
}
int main() {
Student students[STUDENT_NUM];
int total_scores[COURSE_NUM];
float avg_scores[COURSE_NUM];
int fail_count[COURSE_NUM];
for (int i = 0; i < STUDENT_NUM; i++) {
printf("请输入第%d个学生的信息(id, name, score1, score2, score3, score4, score5):\n", i + 1);
scanf("%d%s%d%d%d%d%d", &students[i].id, students[i].name, &students[i].scores[0], &students[i].scores[1],
&students[i].scores[2], &students[i].scores[3], &students[i].scores[4]);
}
calc_total_and_avg(students, total_scores, avg_scores);
calc_fail_count(students, fail_count);
print_result(students, total_scores, avg_scores, fail_count);
return 0;
}
```
这个程序定义了一个 `Student` 结构体,包含学号、姓名和5门课的成绩。在 `main` 函数中,程序从键盘输入了5个学生的信息,并且调用了 `calc_total_and_avg` 和 `calc_fail_count` 函数计算了每门课的总成绩、平均分和未达标人数。最后,程序调用 `print_result` 函数打印出了每个学生的信息、每门课的平均分和未达标人数。
希望这个程序能够满足您的要求。
阅读全文