c语言代码有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的平均成绩,以及平均成绩最高的学生的数据(包括学号、姓名、3门课成绩、平均分数)。
时间: 2023-12-15 16:23:48 浏览: 65
以下是实现代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 10
#define MAX_NAME_LENGTH 20
struct Student {
int id;
char name[MAX_NAME_LENGTH];
float scores[3];
float average_score;
};
void print_highest_average_score_student(struct Student students[]) {
int i, max_index;
float max_average_score = 0.0;
// 找出平均成绩最高的学生
for (i = 0; i < MAX_STUDENTS; i++) {
if (students[i].average_score > max_average_score) {
max_average_score = students[i].average_score;
max_index = i;
}
}
// 打印最高平均成绩的学生数据
printf("平均成绩最高的学生:\n");
printf("学号:%d\n", students[max_index].id);
printf("姓名:%s\n", students[max_index].name);
printf("成绩:%.2f %.2f %.2f\n", students[max_index].scores[0], students[max_index].scores[1], students[max_index].scores[2]);
printf("平均成绩:%.2f\n", students[max_index].average_score);
}
void print_average_scores(struct Student students[]) {
int i, j;
float total_scores[3] = {0.0, 0.0, 0.0};
// 计算每门课程的总成绩
for (i = 0; i < MAX_STUDENTS; i++) {
for (j = 0; j < 3; j++) {
total_scores[j] += students[i].scores[j];
}
}
// 计算每门课程的平均成绩并打印
printf("三门课的平均成绩:");
for (j = 0; j < 3; j++) {
printf("%.2f ", total_scores[j] / MAX_STUDENTS);
}
printf("\n");
}
int main() {
int i, j;
struct Student students[MAX_STUDENTS];
// 从键盘输入学生数据
for (i = 0; i < MAX_STUDENTS; i++) {
printf("请输入第%d个学生的学号、姓名和三门课的成绩:\n", i+1);
scanf("%d %s %f %f %f", &students[i].id, students[i].name, &students[i].scores[0], &students[i].scores[1], &students[i].scores[2]);
students[i].average_score = (students[i].scores[0] + students[i].scores[1] + students[i].scores[2]) / 3;
}
// 打印三门课的平均成绩
print_average_scores(students);
// 打印平均成绩最高的学生数据
print_highest_average_score_student(students);
return 0;
}
```
注意:本代码仅为示例,实际应用中可能需要进行更多的输入检查和错误处理。
阅读全文