交换变量下标问题
#include<stdio.h>struct Student
{
int num; //学号
char name[20]; //名字
float score; //分数
};
int main()
{
struct Student stu[5]=
{
{10010,"Li",80},
{10015,"Po",90},
{10020,"Wu",76},
{10034,"Qi",79},
{10056,"Zhang",89}
};
struct Student temp; //交换用到的结构体变量temp
const int n=5;
int i,j;
printf("the order is:\n");
for(i=0;i<n-1;i++) //按分数高低排序
{
for(j=i+1;j<n;j++)
if(stu[j].score>stu[i].score)
temp=stu[i];stu[i]=stu[j];stu[j]=temp;
//temp=stu[j];stu[j]=stu[i];stu[i]=temp;
}
for(i=0;i<n;i++)
printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
return 0;
}