Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote