c语言有 10 个学生, 每个学生的数据包括学号、 姓名、 3 门课程的成绩, 从键盘输入 10 个学生数据, 要求输出 3 门课程总平均成绩, 以及最高分的学生的数据( 包括学号、 姓 名、 3 门课程成绩、 平均分数)
时间: 2023-12-15 07:25:52 浏览: 72
c语言专业课程设计学生成绩标准管理系统.doc
这是一个计算问题,属于我的范围。以下是C语言代码:
```
#include <stdio.h>
struct student {
int student_id;
char name[20];
float grades[3];
float avg_grade;
};
int main()
{
struct student students[10];
float total_grades[3] = {0};
int max_index = 0;
// input data for each student
for(int i = 0; i < 10; i++) {
printf("Enter data for student %d:\n", i+1);
printf("Student ID: ");
scanf("%d", &students[i].student_id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Grades for 3 courses (separated by spaces): ");
scanf("%f %f %f", &students[i].grades[0], &students[i].grades[1], &students[i].grades[2]);
printf("\n");
// calculate average grade for each student
students[i].avg_grade = (students[i].grades[0] + students[i].grades[1] + students[i].grades[2]) / 3.0;
// update running total for each course
total_grades[0] += students[i].grades[0];
total_grades[1] += students[i].grades[1];
total_grades[2] += students[i].grades[2];
// keep track of student with highest grade
if(students[i].avg_grade > students[max_index].avg_grade) {
max_index = i;
}
}
// calculate overall average grade for each course
float avg_course_grades[3];
avg_course_grades[0] = total_grades[0] / 10.0;
avg_course_grades[1] = total_grades[1] / 10.0;
avg_course_grades[2] = total_grades[2] / 10.0;
// output overall average grade for each course
printf("Overall average grade for each course: %.2f %.2f %.2f\n", avg_course_grades[0], avg_course_grades[1], avg_course_grades[2]);
// output highest grade student's data
printf("Student with highest grade:\n");
printf("Student ID: %d\n", students[max_index].student_id);
printf("Name: %s\n", students[max_index].name);
printf("Grades: %.2f %.2f %.2f\n", students[max_index].grades[0], students[max_index].grades[1], students[max_index].grades[2]);
printf("Average grade: %.2f\n", students[max_index].avg_grade);
return 0;
}
```
阅读全文