I got an assignment to create a program to sort 10 students' data by grade (and if they have the same grade sort by name) using selection sort only(can't use any other method) in an ascending order. My problem is I can sort their grades using selection just fine but I'm having problems sorting the names of students with the same grades, i can use bubble for that but it's not allowed..if anyone could help me out it would be great
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct {
int score;
char name1[20];
} lol[50];
int main()
{
int temp;
int a;
float xy;
char sigh[50];
int arr[] = {2,7,2,9,2,5,13,9,2,22};
char name[50][50] = {"andy","lala","boy","alfred","kurt","shelly","andriy","cooper","chris","scarlet"};
for (int f = 0;f<10;f++){
strcpy (lol[f].name1,name[f]);
lol[f].score = arr[f];
}
puts ("before sorting");
for (int f = 0;f<10;f++){
printf ("%s %d\n",lol[f].name1,lol[f].score);
}
for (int x = 0;x<10;x++){
temp = x;
for (a = x+1;a<10;a++){
if (lol[a].score < lol[temp].score ){
temp = a;
}
}
if (temp!=x){
xy = lol[x].score;
lol[x].score = lol[temp].score;
lol[temp].score = xy;
strcpy(sigh,lol[x].name1);
strcpy(lol[x].name1,lol[temp].name1);
strcpy(lol[temp].name1,sigh);
}
}
for (int x = 1;x<10;x++){
for (a =0;a<9;a++)
{
if (lol[a].score == lol[a+1].score )
{
if (strcmp(lol[a].name1,lol[a+1].name1)>0)
{
strcpy(sigh,lol[a].name1);
strcpy(lol[a].name1,lol[a+1].name1);
strcpy(lol[a+1].name1,sigh);
}
}
}
}
puts("\n");
puts ("after sorting: ");
for (int f = 0;f<10;f++){
printf ("%s %d\n",lol[f].name1,lol[f].score);
}
getch();
}