Say in main you globally declare a string of names (bobby, sally, suzie, joe) an
ID: 3711333 • Letter: S
Question
Say in main you globally declare a string of names (bobby, sally, suzie, joe) and their grades int (73,87,84,73) and you would like to sort their names in order(descending) with their following grade in C++ using a sort function. How would you do this using selection sort or either maxMin sort. Also, how would you sort their ascending ages to correspond with their names? Say in main you globally declare a string of names (bobby, sally, suzie, joe) and their grades int (73,87,84,73) and you would like to sort their names in order(descending) with their following grade in C++ using a sort function. How would you do this using selection sort or either maxMin sort. Also, how would you sort their ascending ages to correspond with their names?Explanation / Answer
#include <iostream>
using namespace std;
int main() {
string names[4] = {"bobby","sally","suzie","joe"};
int grades[4] = {73,87,84,73};
int minIndex;
string minValue;
int minGrade;
for(int i =0; i<4; i++)
{
minIndex = i;
minValue = names[i];
minGrade = grades[i];
for(int index = i+1;index<4;index++)
{
if(names[index] < minValue) // for sorting grades compare grades here
{
minValue = names[index];
minIndex = index;
minGrade = grades[index];
}
}
names[minIndex] = names[i];
names[i] = minValue;
grades[minIndex] = grades[i];
grades[i] = minGrade;
}
for(int i=0;i<4;i++)
{
cout<<names[i]<<" "<<grades[i]<<endl;
}
return 0;
}
output:
bobby 73
joe 73
sally 87
suzie 84
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.