用c语言有4个学生,每个学生的数据包括学号、姓名、3门课程的成绩。从键盘输入10个学生的数据,要求输出3门课程的总平均成绩,以及最高分的学生的学号、姓名、3门课程成绩、平均分数。
时间: 2023-12-15 20:59:48 浏览: 83
以下是用C语言实现的代码:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 4
#define NUM_COURSES 3
struct student {
int id;
char name[50];
float grades[NUM_COURSES];
float avg_grade;
};
int main() {
struct student students[NUM_STUDENTS];
float total_grades[NUM_COURSES] = {0};
float highest_avg_grade = 0;
int highest_avg_grade_index = 0;
// Input student data
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Enter student %d's id: ", i+1);
scanf("%d", &students[i].id);
printf("Enter student %d's name: ", i+1);
scanf("%s", students[i].name);
for (int j = 0; j < NUM_COURSES; j++) {
printf("Enter student %d's grade for course %d: ", i+1, j+1);
scanf("%f", &students[i].grades[j]);
total_grades[j] += students[i].grades[j];
}
students[i].avg_grade = (students[i].grades[0] + students[i].grades[1] + students[i].grades[2]) / 3;
if (students[i].avg_grade > highest_avg_grade) {
highest_avg_grade = students[i].avg_grade;
highest_avg_grade_index = i;
}
}
// Calculate average grade for each course
float avg_grades[NUM_COURSES];
for (int i = 0; i < NUM_COURSES; i++) {
avg_grades[i] = total_grades[i] / NUM_STUDENTS;
}
// Output results
printf("Average grade for each course:\n");
for (int i = 0; i < NUM_COURSES; i++) {
printf("Course %d: %.2f\n", i+1, avg_grades[i]);
}
printf("\n");
printf("Student with highest average grade:\n");
printf("Id: %d\n", students[highest_avg_grade_index].id);
printf("Name: %s\n", students[highest_avg_grade_index].name);
printf("Grades: %.2f %.2f %.2f\n", students[highest_avg_grade_index].grades[0], students[highest_avg_grade_index].grades[1], students[highest_avg_grade_index].grades[2]);
printf("Average grade: %.2f\n", students[highest_avg_grade_index].avg_grade);
return 0;
}
```
代码说明:
1. 定义了一个结构体 `student`,包含学号、姓名、3门课程的成绩和平均成绩。
2. 定义了常量 `NUM_STUDENTS` 和 `NUM_COURSES`,分别代表学生人数和课程数。
3. 定义了一个数组 `students`,用于存储学生的数据。
4. 定义了一个数组 `total_grades`,用于存储每门课程的总成绩。
5. 定义了变量 `highest_avg_grade` 和 `highest_avg_grade_index`,用于存储最高平均成绩和对应的学生下标。
6. 通过循环从键盘输入学生的数据,并计算每个学生的平均成绩、每门课程的总成绩和最高平均成绩。
7. 计算每门课程的平均成绩,存储在数组 `avg_grades` 中。
8. 输出每门课程的平均成绩和最高平均成绩对应的学生的数据。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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)