#include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 30 //最大学生个数 #define MAX_COURSE_NUM 6 //最大课程个数 #define NAME_LEN 20 //最大名字个数 typedef struct Student { char id[10];//学生学号 char name[NAME_LEN];//姓名 int scores[MAX_COURSE_NUM];//各科分数 int total_score;//总分 float avg_score;//平均分 } XUESHENG;//将结构体名称命名为XUESHENG void calc_total_avg_student(XUESHENG students[], int num, int course_num){//计算输出每个学生的总分和平均分 请补充函数
时间: 2023-12-22 22:05:21 浏览: 27
void calc_total_avg_student(XUESHENG students[], int num, int course_num){
for(int i=0; i<num; i++){
int total_score = 0;
for(int j=0; j<course_num; j++){
total_score += students[i].scores[j];
}
students[i].total_score = total_score;
students[i].avg_score = (float)total_score / course_num;
printf("学号:%s,姓名:%s,总分:%d,平均分:%.2f\n", students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
该函数可以计算每个学生的总分和平均分,并输出每个学生的信息。其中,参数students是一个结构体数组,表示所有学生的信息;参数num表示学生的数量;参数course_num表示课程的数量。函数会遍历所有学生,计算他们的总分和平均分,存储在对应的结构体中,并输出每个学生的信息。
相关问题
#include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 30 //最大学生个数 #define MAX_COURSE_NUM 6 //最大课程个数 #define NAME_LEN 20 //最大名字个数 typedef struct Student { char id[10];//学生学号 char name[NAME_LEN];//姓名 int scores[MAX_COURSE_NUM];//各科分数 int total_score;//总分 float avg_score;//平均分 } XUESHENG;//将结构体名称命名为XUESHENG void calc_total_avg_student(XUESHENG students[], int num, int course_num){//计算每个学生的总分和平均分 请补充函数
void calc_total_avg_student(XUESHENG students[], int num, int course_num){
for(int i=0; i<num; i++){
int total_score = 0;
for(int j=0; j<course_num; j++){
total_score += students[i].scores[j];
}
students[i].total_score = total_score;
students[i].avg_score = (float)total_score / course_num;
}
}
该函数可以计算每个学生的总分和平均分。其中,参数students是一个结构体数组,表示所有学生的信息;参数num表示学生的数量;参数course_num表示课程的数量。函数会遍历所有学生,计算他们的总分和平均分,存储在对应的结构体中。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 // 学生信息结构体 typedef struct { char num[10]; // 学号 char name[10]; // 姓名 char clas[10]; // 班级 char sex[10]; // 性别 float score; // 成绩 } Student; // 学生信息数组 Student studentList[MAX_SIZE]; int size = 0; 帮我添加一个可以查询学生成绩最大值和最小值的分函数代码
可以添加以下代码来实现查询学生成绩最大值和最小值的函数:
```c
// 查询学生成绩最大值和最小值
void findMinMaxScore() {
float maxScore = 0, minScore = 100;
for (int i = 0; i < size; i++) {
if (studentList[i].score > maxScore) {
maxScore = studentList[i].score;
}
if (studentList[i].score < minScore) {
minScore = studentList[i].score;
}
}
printf("成绩最高的学生信息:\n");
for (int i = 0; i < size; i++) {
if (studentList[i].score == maxScore) {
printf("学号:%s 姓名:%s 班级:%s 性别:%s 成绩:%.2f\n",
studentList[i].num, studentList[i].name,
studentList[i].clas, studentList[i].sex,
studentList[i].score);
}
}
printf("成绩最低的学生信息:\n");
for (int i = 0; i < size; i++) {
if (studentList[i].score == minScore) {
printf("学号:%s 姓名:%s 班级:%s 性别:%s 成绩:%.2f\n",
studentList[i].num, studentList[i].name,
studentList[i].clas, studentList[i].sex,
studentList[i].score);
}
}
}
```
在主函数中,可以通过调用该函数来查询学生成绩最大值和最小值:
```c
int main() {
// 添加学生信息
// ...
// 查询学生成绩最大值和最小值
findMinMaxScore();
return 0;
}
```