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

This problem is three runner in C++ Write a program that asks the user the names

ID: 3582100 • Letter: T

Question

This problem is three runner in C++ Write a program that asks the user the names of four runners and the time it took each of them to finish a race. The program should display who came in first, second third place, and fourth place. The program should also consider the possibility of two or more runners finishing with the same time. The runners' times are between 1 and 10. The runners' names however are unique. Note: if all the runners have the same time then all the runners come in first places. Note: think about how you can use the dual sort function to solve this problem.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

void sortScores (double scores[], int num, string names[])
{
int i, minIndex;
double minValue;
string tempName;

for (i = 0; i < (num - 1); i++) //Outer loop to get the first pivot element
{
minIndex = i;
minValue = scores[i];
tempName = names[i];
for (int index = (i + 1); index < num; index++) //Inner loop to get the second pivot element
{
if (scores[index] < minValue)           //if current value is lesser than min value then
{
minValue = scores[index];           //considering the current value is min value
tempName = names[index];
minIndex = index;
}
}                           //Swapping the values after completing the inner loop iteration
scores[minIndex] = scores[i];
names[minIndex] = names[i];
scores[i] = minValue;
names[i] = tempName;
}
}

int main(){
   int n = 4;
   string names[n],pos[] = {"First","Second","Third","Fourth"}; // declaring names array and initializing pos array which contains                                    positions names
   double scores[n];
   cout << "Enter names :";
   for(int i=0;i<n;i++){
       cin >> names[i];
   }
   cout << "Enter scores :";
   for(int i=0;i<n;i++){
       cin >> scores[i];
   }
   sortScores(scores,4,names);    //Calling function to sort the names and numbers using dual sort
   int first = scores[0],position=1;
   cout << pos[0]+" Place : ";
   for(int i=0;i<n;i++){       //Loop to print the results in appropriate manner
       if(scores[i] != first){ //Checking whether there are multiple players for same position and if not then
           cout << " " + pos[position++] + " Place : "; //Printing the next position to the screen
           first = scores[i];
       }
       cout << names[i]+" "; //Prtinting the name of the runner
   }
   cout << endl;
}

//Sample Input
//Enter Names : "Chegg","Query","Runner","Me"
//Enter Scores : 7 5 2 5

//Sample output
//first place : Runner
//second place : Query Me
//Third place : Chegg

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