I made the following structure in C++: struct studentType { string firstname; st
ID: 3620394 • Letter: I
Question
I made the following structure in C++:struct studentType
{
string firstname;
string lastname;
double midterm;
double final;
double homework[5];
double hmavg;
double average;
char grade;
}; and I made an array of the previous structure, and in one of the functions I have to sort the array according to the grade, but if the grade is the same, the array is to be sorted alphabetically by firstname, I only managed the first part but tried to do the second part when the grades are the same, but it didn't work!
following is the function:
void arrangeByGrades(studentType *s, int n)
{
studentType min;
int indexMin;
for(int i=0; i<n-1; i++)
{
min.grade = s[i].grade;
for(int j=i+1; j<n; j++)
{
if(s[j].grade<min.grade)
{
min.grade = s[j].grade;
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
}
}
} Please help me complete the function!, I've already tried this but it didn't work: void arrangeByGrades(studentType *s, int n)
{
studentType min;
int indexMin;
for(int i=0; i<n-1; i++)
{
min.grade = s[i].grade;
for(int j=i+1; j<n; j++)
{
if(s[j].grade<min.grade)
{
min.grade = s[j].grade;
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
else if (s[j].grade == min.grade)
{
if (s[i].firstname[0]< s[j].firstname[0])
{
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
}
}
}
}
}; and I made an array of the previous structure, and in one of the functions I have to sort the array according to the grade, but if the grade is the same, the array is to be sorted alphabetically by firstname, I only managed the first part but tried to do the second part when the grades are the same, but it didn't work!
following is the function:
void arrangeByGrades(studentType *s, int n)
{
studentType min;
int indexMin;
for(int i=0; i<n-1; i++)
{
min.grade = s[i].grade;
for(int j=i+1; j<n; j++)
{
if(s[j].grade<min.grade)
{
min.grade = s[j].grade;
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
}
}
} void arrangeByGrades(studentType *s, int n)
{
studentType min;
int indexMin;
for(int i=0; i<n-1; i++)
{
min.grade = s[i].grade;
for(int j=i+1; j<n; j++)
{
if(s[j].grade<min.grade)
{
min.grade = s[j].grade;
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
}
}
} Please help me complete the function!, I've already tried this but it didn't work: void arrangeByGrades(studentType *s, int n)
{
studentType min;
int indexMin;
for(int i=0; i<n-1; i++)
{
min.grade = s[i].grade;
for(int j=i+1; j<n; j++)
{
if(s[j].grade<min.grade)
{
min.grade = s[j].grade;
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
else if (s[j].grade == min.grade)
{
if (s[i].firstname[0]< s[j].firstname[0])
{
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
}
}
}
} void arrangeByGrades(studentType *s, int n)
{
studentType min;
int indexMin;
for(int i=0; i<n-1; i++)
{
min.grade = s[i].grade;
for(int j=i+1; j<n; j++)
{
if(s[j].grade<min.grade)
{
min.grade = s[j].grade;
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
else if (s[j].grade == min.grade)
{
if (s[i].firstname[0]< s[j].firstname[0])
{
indexMin = j;
studentType temp = s[i];
s[i] = s[indexMin];
s[indexMin] = temp;
}
}
}
}
}
Explanation / Answer
if(s[i].firstname.compare(s[j].firstname) < 0)Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.