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

cpp A group of students decided to run in the Columbus Marathon. Their names and

ID: 3683830 • Letter: C

Question

cpp A group of students decided to run in the Columbus Marathon. Their names and times (in minutes) are below:

Name          Time (minutes)

Elena

341

Thomas

273

Hamilton

278

Suzie

329

Phil

445

Matt

402

Alex

388

Emma

275

John

243

James

334

Jane

412

Emily

393

Daniel

299

Neda

343

Aaron

317

Kate

265

Find the fastest two runners.

In particular, write a program as follows. Print the list of runners and times as above. Then print the name of the fastest runner and his/her time (in hours and minutes). Also, find the second fastest runner. Print the name and his/her time (in hours and minutes).

The program must have a method that takes as input an array of integers and returns the index corresponding to the person with the lowest time. The Program must also have a second method to find the second-best runner. The second method can use the first method to determine the best runner, and then return the index corresponding to the person with the second lowest time.

Extra Credit (10 points): Print the list of runners and times nicely formatted in columns as displayed above.

Here is some program code to get started:

#include <iostream>

#include <string>

using namespace std;

int main (){

   const int numRunners = 16;

   string names[] ={"Elena", "Thomas", "Hamilton", "Suzie", "Phil",

      "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel",

      "Neda","Aaron", "Kate"};

   int times[] ={341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412,

       393, 299,343, 317, 265};

   . . .

   for (int i = 0; i < numRunners; i++) {

       cout << names[i] << ": " << times[i];

}}

Elena

341

Thomas

273

Hamilton

278

Suzie

329

Phil

445

Matt

402

Alex

388

Emma

275

John

243

James

334

Jane

412

Emily

393

Daniel

299

Neda

343

Aaron

317

Kate

265

Explanation / Answer

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

int findWinner(int times[])
{
    int min=999,minindex=-1;
    for(int i=0;i<16;i++)
    {
        if(times[i]<min)
        {
        minindex=i;
        min=times[i];
        }
    }
    return minindex;
}

int findRunnerUp(int times[])
{
    int winnerindex=findWinner(times);
    int min=999,minindex=-1;
    for(int i=0;i<16;i++)
    {
        if(times[i] -times[winnerindex]<min && times[i] -times[winnerindex]!=0)
        {
        minindex=i;
        min=times[i];
        }
    }
    return minindex;
}

int main (){

   const int numRunners = 16;
   int winner,runnerup;

   string names[] ={"Elena", "Thomas", "Hamilton", "Suzie", "Phil",

      "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel",

      "Neda","Aaron", "Kate"};

   int times[] ={341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412,

       393, 299,343, 317, 265};

   for (int i = 0; i < numRunners; i++) {

       cout << names[i] << " " << times[i]<<endl;

}
  
    cout<< "Fastest runner :"<<" " <<names[findWinner(times)] << " "<<times[findWinner(times)]<<endl;
    cout<<"Runner up: "<<" " <<names[findRunnerUp(times)] << " " << times[findRunnerUp(times)]<<endl;
    return 0;
  
}