I have two structures
struct StudentRec {
char name[25]; // Name of student
float average; // Student's average
};
struct Roster {
StudentRec student[100];
int numStudent;
};
I have this function that I'm trying to sort the names in order:
void sort(Roster& r)
{
for (int k =0; k < r.numStudent - 1; k++){
int next = k;
for(int j = k+1; j < r.numStudent; j++)
if(j < next)
next = j;
StudentRec temp; //Swapping buffer
strcpy(temp.name, r.student[next].name);
strcpy(r.student[next].name,r.student[k].name);
strcpy(r.student[k].name,temp.name);
}
}
I know it will fail at the if statement and I have tried next < j but that will only sort one or two. Any suggestions will be greatly appreciated. Have a nice night.
struct StudentRec {
char name[25]; // Name of student
float average; // Student's average
};
struct Roster {
StudentRec student[100];
int numStudent;
};
I have this function that I'm trying to sort the names in order:
void sort(Roster& r)
{
for (int k =0; k < r.numStudent - 1; k++){
int next = k;
for(int j = k+1; j < r.numStudent; j++)
if(j < next)
next = j;
StudentRec temp; //Swapping buffer
strcpy(temp.name, r.student[next].name);
strcpy(r.student[next].name,r.student[k].name);
strcpy(r.student[k].name,temp.name);
}
}
I know it will fail at the if statement and I have tried next < j but that will only sort one or two. Any suggestions will be greatly appreciated. Have a nice night.