利用c语言用结构体数组录取学生信息:学号,名字,各科成绩,平均分,最低和最高分
时间: 2023-04-11 21:02:08 浏览: 155
C语言用数组来对学生成绩进行处理
可以使用以下代码实现:
```c
#include <stdio.h>
struct student {
int id;
char name[50];
float scores[5];
float average;
float min_score;
float max_score;
};
int main() {
struct student students[10];
int i, j;
// 录入学生信息
for (i = 0; i < 10; i++) {
printf("请输入第%d个学生的信息:\n", i+1);
printf("学号:");
scanf("%d", &students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("各科成绩:");
for (j = 0; j < 5; j++) {
scanf("%f", &students[i].scores[j]);
}
}
// 计算平均分、最低分和最高分
for (i = 0; i < 10; i++) {
float sum = 0;
students[i].min_score = students[i].scores[0];
students[i].max_score = students[i].scores[0];
for (j = 0; j < 5; j++) {
sum += students[i].scores[j];
if (students[i].scores[j] < students[i].min_score) {
students[i].min_score = students[i].scores[j];
}
if (students[i].scores[j] > students[i].max_score) {
students[i].max_score = students[i].scores[j];
}
}
students[i].average = sum / 5;
}
// 输出学生信息
printf("学号\t姓名\t各科成绩\t平均分\t最低分\t最高分\n");
for (i = 0; i < 10; i++) {
printf("%d\t%s\t", students[i].id, students[i].name);
for (j = 0; j < 5; j++) {
printf("%.1f\t", students[i].scores[j]);
}
printf("%.1f\t%.1f\t%.1f\n", students[i].average, students[i].min_score, students[i].max_score);
}
return 0;
}
```
这段代码使用了结构体数组来存储学生信息,包括学号、姓名、各科成绩、平均分、最低分和最高分。通过循环录入学生信息,然后计算平均分、最低分和最高分,最后输出学生信息。
阅读全文