to Arrange The Records of A Structure in Ascending Order (Bubble Sort)
to Arrange The Records of A Structure in Ascending Order (Bubble Sort)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct game
{ int gcode;
char gname[20];
float score;
};
void bsort(game g[10],int size)
{ game temp;
int i,j;
for(i=0;i<size;i++)
{for(j=0;j<size-1-i;j++)
{if(g[j].score>g[j+1].score)
{temp=g[i];
g[i]=g[j+1];
g[j+1]=temp;
}
}
}
cout<<"gcode\tgname\tscore\n";
for(i=0;i<size;i++)
cout<<g[i].gcode<<"\t"<<g[i].gname<<"\t"<<g[i].score<<endl;
}
void main()
{ clrscr();
game g[10];
int size,i;
cout<<"Enter size of array";
cin>>size;
cout<<"Enter array element";
for(i=0;i<size;i++)
{cout<<"Enter gcode";
cin>>g[i].gcode;
cout<<"Enter gname";
gets(g[i].gname);
cout<<"Enter score";
cin>>g[i].score;
}
bsort(g,size);
getch();
}