在主函数分别输入1班10名学生和2班12名学生某门功课成绩,分别调用这两个函数,求出两个班级该门功课成绩最高分和最高分的学号并输出,同时将两个班级的成绩从高到底进行排序并分别输出,c 语言程序
时间: 2024-03-09 10:50:42 浏览: 61
好的,以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENT_NUM 30
typedef struct {
int id;
float score;
} Student;
void inputScores(int classNum, Student students[]) {
printf("请输入第%d班的学生成绩:\n", classNum);
for (int i = 0; i < MAX_STUDENT_NUM; i++) {
printf("请输入第%d名学生的成绩(输入-1结束):", i + 1);
float score;
scanf("%f", &score);
if (score < 0) {
break;
}
students[i].id = i + 1;
students[i].score = score;
}
}
void findMaxScore(int classNum, Student students[]) {
int maxId = 0;
float maxScore = students[0].score;
for (int i = 1; i < MAX_STUDENT_NUM && students[i].score >= 0; i++) {
if (students[i].score > maxScore) {
maxId = i;
maxScore = students[i].score;
}
}
printf("第%d班成绩最高的学生是第%d名,分数为%.2f。\n", classNum, students[maxId].id, maxScore);
}
void sortScores(int classNum, Student students[]) {
int i, j;
for (i = 0; i < MAX_STUDENT_NUM && students[i].score >= 0; i++) {
for (j = i + 1; j < MAX_STUDENT_NUM && students[j].score >= 0; j++) {
if (students[i].score < students[j].score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("第%d班成绩从高到低排序为:\n", classNum);
for (int k = 0; k < i; k++) {
printf("第%d名学生的成绩为%.2f。\n", students[k].id, students[k].score);
}
}
int main() {
Student class1[MAX_STUDENT_NUM], class2[MAX_STUDENT_NUM];
inputScores(1, class1);
inputScores(2, class2);
findMaxScore(1, class1);
findMaxScore(2, class2);
sortScores(1, class1);
sortScores(2, class2);
return 0;
}
```
注意事项:
1. 本程序使用了结构体来存储学生的信息,其中包括学号和成绩两个字段;
2. `inputScores` 函数和 `sortScores` 函数均采用了类似于冒泡排序的算法;
3. 为了方便输入,本程序规定当输入成绩为负数时结束输入。
阅读全文