用c语言定义一个结构体数组,存放10个学生的学号,姓名,三门课的成绩 2.从键盘输入10个学生的以上内容 3.输出单门课成绩最高的学生的学号、姓名、以及该门课程的成绩 4.输出三门课程的平均分数最高的学生的学号、姓名及其平均分 5.将10个学生按照平均分数从高到低进行排序,输出结果,
时间: 2023-11-28 16:49:53 浏览: 224
运用结构体数组与输入函数进行存放学生的学号姓名等数据
4星 · 用户满意度95%
以下是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 10
#define NUM_COURSES 3
struct student {
int id;
char name[20];
int scores[NUM_COURSES];
float avg_score;
};
void input_students(struct student students[]);
void print_highest_score(struct student students[]);
void print_highest_avg(struct student students[]);
void sort_students(struct student students[]);
int main() {
struct student students[NUM_STUDENTS];
input_students(students);
print_highest_score(students);
print_highest_avg(students);
sort_students(students);
printf("\nRanking of students by average score:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("%d. %s (ID: %d) - Average Score: %.2f\n", i+1, students[i].name, students[i].id, students[i].avg_score);
}
return 0;
}
void input_students(struct student students[]) {
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Enter information for student %d:\n", i+1);
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
for (int j = 0; j < NUM_COURSES; j++) {
printf("Score for course %d: ", j+1);
scanf("%d", &students[i].scores[j]);
}
float sum_scores = 0;
for (int j = 0; j < NUM_COURSES; j++) {
sum_scores += students[i].scores[j];
}
students[i].avg_score = sum_scores / NUM_COURSES;
}
}
void print_highest_score(struct student students[]) {
int highest_score = 0;
struct student highest_scoring_student;
for (int i = 0; i < NUM_STUDENTS; i++) {
for (int j = 0; j < NUM_COURSES; j++) {
if (students[i].scores[j] > highest_score) {
highest_score = students[i].scores[j];
highest_scoring_student = students[i];
}
}
}
printf("\nStudent with highest score:\n");
printf("Name: %s (ID: %d)\n", highest_scoring_student.name, highest_scoring_student.id);
printf("Highest Score: %d\n", highest_score);
}
void print_highest_avg(struct student students[]) {
float highest_avg = 0;
struct student highest_avg_student;
for (int i = 0; i < NUM_STUDENTS; i++) {
if (students[i].avg_score > highest_avg) {
highest_avg = students[i].avg_score;
highest_avg_student = students[i];
}
}
printf("\nStudent with highest average score:\n");
printf("Name: %s (ID: %d)\n", highest_avg_student.name, highest_avg_student.id);
printf("Average Score: %.2f\n", highest_avg);
}
void sort_students(struct student students[]) {
struct student temp;
for (int i = 0; i < NUM_STUDENTS; i++) {
for (int j = i+1; j < NUM_STUDENTS; j++) {
if (students[j].avg_score > students[i].avg_score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
```
该程序定义了一个结构体 `student`,包括学号、姓名、三门课的成绩和平均分数。通过 `input_students()` 函数从键盘输入学生信息,计算平均分数。然后,通过 `print_highest_score()` 函数输出单门课成绩最高的学生信息,通过 `print_highest_avg()` 函数输出三门课程的平均分数最高的学生信息,最后通过 `sort_students()` 函数将学生按照平均分数从高到低进行排序,并输出排名结果。
阅读全文