用c语言写出:先输入6个学生的信息(包括学号、姓名、成绩),然后要求按照成绩从低到高的顺序输出各学生的信息,最后再输出6个学生的平均成绩。
时间: 2024-03-25 14:41:27 浏览: 10
好的,以下是代码实现:
```c
#include <stdio.h>
#include <string.h>
// 学生信息结构体
struct Student {
char id[10]; // 学号
char name[20]; // 姓名
float score; // 成绩
};
int main() {
// 输入学生信息
struct Student stu[6];
for(int i = 0; i < 6; i++) {
printf("请输入第%d个学生的学号、姓名、成绩:\n", i+1);
scanf("%s%s%f", stu[i].id, stu[i].name, &stu[i].score);
}
// 冒泡排序,按成绩从低到高排序
for(int i = 0; i < 6; i++) {
for(int j = i+1; j < 6; j++) {
if(stu[i].score > stu[j].score) {
struct Student temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
// 输出排序后的学生信息
printf("按成绩从低到高排序后的学生信息如下:\n");
for(int i = 0; i < 6; i++) {
printf("%s\t%s\t%.1f\n", stu[i].id, stu[i].name, stu[i].score);
}
// 计算平均成绩并输出
float sum = 0;
for(int i = 0; i < 6; i++) {
sum += stu[i].score;
}
printf("6个学生的平均成绩为:%.1f\n", sum/6);
return 0;
}
```
运行程序后,按照提示输入6个学生的信息,程序会自动按照成绩从低到高的顺序输出各学生的信息,并计算并输出6个学生的平均成绩。