|
发表于 2020-4-8 19:52:50
|
显示全部楼层
本帖最后由 sunrise085 于 2020-4-8 19:59 编辑
程序第49~51行,交换两个student的时候写错了
交换的应该是i和index,你写成交换i和j了
另外,不管在一次内循环中,有没有找到比当前student[i]大的值,你都会进行交换,这是不对的
- #include<stdio.h>
- struct student
- {
- int num;
- char name[10];
- int computer,english,math;
- double average;
- };
- int main()
- {
- struct student students[50],temp;
- int n,j,i,index;
- printf("请输入你要输入的学生个数:");
- scanf("%d",&n);
- for(i = 0; i < n; i++)
- {
- printf("Input the info of No.%d:",i+1);
- scanf("%d",&students[i].num);
- printf("Input the name of student %d:",i+1);
- scanf("%s",students[i].name);
- printf("Input math score:");
- scanf("%d",&students[i].math);
- printf("Input english score:");
- scanf("%d",&students[i].english);
- printf("Input computer score:");
- scanf("%d",&students[i].computer);
- students[i].average = (students[i].math + students[i].english + students[i].computer) / 3.0;
- }
- for(i = 0; i < n-1; i++)
- {
- for(j = i+1; j < n; j++)
- {
- if(students[j].average > students[i].average)
- {
- temp = students[j];
- students[j] = students[i];
- students[i] = temp;
- }
- }
- }
- printf("num\tname\taverage\t");
- printf("\n");
- for(i = 0; i < n; i++)
- {
- printf("%d\t%s\t%.2lf\n",students[i].num,students[i].name,students[i].average);
- }
- }
复制代码 |
|