有6个学生,每个学生的数据包括学号、姓名、3门课的成绩,编写函数,实现功能:(1)从键盘输入学生信息,存入到一个结构体数组中;(2)计算学生三门课程的平均成绩,并存入到结构体数组中;(3)打印出每门课的班级平均成绩;(4)打印出最高分的学生的数据(包括学号、姓名、 3 门课的成绩)。要求: 设计一个能保存学生信息的结构,包括学生的学号,姓名,成绩数组和平均分;在主函数中输入学生的学号,姓名和三门课成绩;在主函数中调用各函数实现功能,并显示相应结果。c语言代码
时间: 2023-12-15 11:14:48 浏览: 101
以下是实现上述功能的 C 语言代码:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 6 // 学生人数
#define NUM_COURSES 3 // 课程数
// 学生结构体
typedef struct {
char id[20]; // 学号
char name[20]; // 姓名
int scores[NUM_COURSES]; // 成绩数组
float avg_score; // 平均分
} Student;
// 从键盘输入学生信息
void input_students(Student students[]) {
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("请输入第%d个学生的学号、姓名和三门课成绩:\n", i + 1);
scanf("%s%s%d%d%d", students[i].id, students[i].name,
&students[i].scores[0], &students[i].scores[1], &students[i].scores[2]);
}
}
// 计算学生三门课程的平均成绩
void calc_avg_score(Student students[]) {
for (int i = 0; i < NUM_STUDENTS; i++) {
int sum = 0;
for (int j = 0; j < NUM_COURSES; j++) {
sum += students[i].scores[j];
}
students[i].avg_score = (float) sum / NUM_COURSES;
}
}
// 打印出每门课的班级平均成绩
void print_course_avg_score(Student students[]) {
printf("每门课的班级平均成绩为:\n");
for (int i = 0; i < NUM_COURSES; i++) {
int sum = 0;
for (int j = 0; j < NUM_STUDENTS; j++) {
sum += students[j].scores[i];
}
printf("第%d门课的平均分为:%.2f\n", i + 1, (float) sum / NUM_STUDENTS);
}
}
// 打印出最高分的学生的数据
void print_highest_score_student(Student students[]) {
int max_score = 0;
int max_index = 0;
for (int i = 0; i < NUM_STUDENTS; i++) {
int sum = 0;
for (int j = 0; j < NUM_COURSES; j++) {
sum += students[i].scores[j];
}
if (sum > max_score) {
max_score = sum;
max_index = i;
}
}
printf("最高分的学生信息为:\n");
printf("学号:%s\n", students[max_index].id);
printf("姓名:%s\n", students[max_index].name);
printf("成绩:");
for (int i = 0; i < NUM_COURSES; i++) {
printf("%d ", students[max_index].scores[i]);
}
printf("\n");
}
int main() {
Student students[NUM_STUDENTS]; // 学生数组
input_students(students); // 输入学生信息
calc_avg_score(students); // 计算学生平均分
print_course_avg_score(students); // 打印每门课的班级平均成绩
print_highest_score_student(students); // 打印最高分的学生信息
return 0;
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)