【发布时间】:2020-12-31 13:15:14
【问题描述】:
我遇到了一个问题,我用不同的方法尝试了很多次,但仍然无法获得所需的解决方案,请帮助我。 问题:定义一个结构来存储学生的详细信息。将它们传递给一个函数,其中 CGPA 最高的学生是从一组 5 名学生中计算出来的。并显示结果。(这里我们必须存储每个学生的姓名、年龄和 CGPA) 这是我对这个问题的尝试:
#include <stdio.h>
void highCGPA(float b,struct detst D[4]);
struct detst
{
int age;
float CGPA;
char name[30];
};
int main()
{
struct detst D[4];
int i;
float h;
for(i=0;i<=4;i++)
{
printf("Enter the name of student %d:\n",i+1);
scanf("%s",&D[i].name);
printf("Enter the age of student %d:\n",i+1);
scanf("%d",&D[i].age);
printf("Enter the CGPA obtined by student %d:\n",i+1);
scanf("%f",&D[i].CGPA);
}
highCGPA(h,D);
}
void highCGPA(float b,struct detst D[4])
{
int i,max;
max = D[0].CGPA;
for(i=0;i<=4;i++)
{
if(D[i].CGPA > max)
{
max = D[i].CGPA;
}
}
printf("Highest CGPA obtained is:\n%f",max);
}
【问题讨论】:
-
i <= 4??可能应该考虑制作这个i < 4 -
你有什么问题?
标签: c structure user-defined-functions