问题遇到的现象和发生背景
有n个结构体变量,内含学生号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息
想要知道错在哪
用代码块功能插入代码,请勿粘贴截图
#pragma warning(disable : 4996)
#include<stdio.h>
#define N 3 //三个学生
struct Student
{
int num;
char name[10];
float score[3]; //三门课程
double aver;
};
int main()
{
void input(struct Student s1[]);
struct Student max(struct Student s2[]);
void print(struct Student st);
struct Student stu[N];
struct Student* p;
struct Student pt;
p = stu;
input(p);
pt = max(p);
print(pt);
return 0;
}
void input(struct Student s1[])
{
int i;
printf("输入3位学生的信息:\n");
for (i = 0; i < N; i++)
{
scanf("%d %s %f %f %f\n", &s1[i].num, &s1[i].name, &s1[i].score[0], &s1[i].score[1], &s1[i].score[2]); //输入各学生的信息
s1[i].aver = s1[i].score[0] + s1[i].score[1] + s1[i].score[2] / 3.0; //计算各学生的平均成绩
}
}
struct Student max(struct Student s2[]) //找出平均成绩最高的学生
{
int i;
int m = 0;
for (i = 0; i < N; i++)
{
if (s2[i].aver > s2[m].aver)
{
m = i;
}
}
return (s2[m]);
}
void print(struct Student st) //输出平均成绩最高的学生
{
printf("平均成绩最高的学生是:\n");
printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\n", st.num, st.name, st.score[0], st.score[1], st.score[2],st.aver);
}
运行结果及报错内容
只有一个警告:返回值被忽略:scanf
我输入3个学生信息后,系统不输出平均成绩最高的学生