注册 登录
编程论坛 VC++/MFC

简单的程序,可是到底错那了,输不出来

笨小孩也可以 发布于 2015-08-23 15:29, 2162 次点击
#include"stdio.h"
#define N 5
struct student
{long stu_num; char sex;
char name[15]; int score[4];
float average;
};
main()
{int I,j,k,t; float sum,max;
struct student stuarr[N];
printf("please input student information:\n");
for(I=0;I<=5;I++)
scanf("%ld,%c,%s",&stuarr[I].stu_num,&stuarr[I].sex,stuarr[I].name);
for(j=0;j<4;j++)
scanf("%d",&stuarr[I].score[j]);
for(sum=0,j=0;j<4;j++)
{sum+=stuarr[I].score[j];
stuarr[I].average=sum/4;
printf("%lf",stuarr[I].average);
}
max=stuarr[0].average;
for(I=1;I<=5;I++)
{if(stuarr[I].average>max)
max=stuarr[I].average;
}
printf("总平均成绩最高的学生信息是学号=%ld,性别=%c,姓名=%s,平均成绩=%4.1f\n",stuarr[I].stu_num,stuarr[I].sex,stuarr[I].name,stuarr[I].average);
}
2 回复
#2
hjx11202015-08-23 17:01
C语言和C++本质上还是有区别的,介个写的是那种语言都还没分清楚,找出错误确实挺难的
#3
fuyong02272015-08-23 17:13
#include"stdio.h"
#define N 5
struct student
{long stu_num; char sex;
char name[15]; int score[4];
float average;
};
void main()
{
    int I,j; float sum,max;
    struct student stuarr[N];
    printf("please input student information:\n");
    for(I=0;I<5;I++)
    {
        scanf("%ld %c %s",&stuarr[I].stu_num,&stuarr[I].sex,stuarr[I].name);
        for(j=0;j<4;j++)
            scanf("%d",&stuarr[I].score[j]);
        for(sum=0,j=0;j<4;j++)
        {
            sum+=stuarr[I].score[j];
            stuarr[I].average=sum/4;
        }
        // printf("%lf",stuarr[I].average);
    }
    max=stuarr[0].average;
    int maxIndex = 0;
    for(I=0;I<5;I++)
    {
        if(stuarr[I].average>max)
        {
            max=stuarr[I].average;
            maxIndex = I;
        }
    }
    printf("总平均成绩最高的学生信息是学号=%ld,性别=%c,姓名=%s,平均成绩=%4.1f\n",stuarr[maxIndex].stu_num,stuarr[maxIndex].sex,stuarr[maxIndex].name,stuarr[maxIndex].average);
}
输出结果:
please input student information:
1 1 张 79 83 88 93
2 1 李 88 78 78 92
3 2 王 79 56 93 85
4 2 杨 77 79 84 83
5 2 赵 97 78 99 100
总平均成绩最高的学生信息是学号=5,性别=2,姓名=赵,平均成绩=93.5
Press any key to continue
1