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

FastestRunner.cpp A group of students decided to run in the Columbus Marathon. T

ID: 646411 • Letter: F

Question

FastestRunner.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 runner.

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 should 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 should apply this method to the array of running times to find the fastest runner.   Also include a second method to find the second-best runner. The second method should use the first method to determine the best runner, and then returns 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];

}}

NOTE: The program standards are given below:

1. Program Header .   In addition to you name and project number, include a brief description of the program functionality in the header comment for the program

2. Variables and Constants.    All variables and constants in a program must be declared at the beginning of the program

3. Initialization.   Variables may not be initialized in their declaration.

4. Printing of if-then.   Use one of the following

     if (expression) statement

     if (expression)

        Single-statement

     if (expression) {

        Multiple-statements

     }

5. Printing of if-then-else. Use one of the following

     if (expression) statement

     else            statement

     if (expression)

        Single-statement

     else

        Single-statement

     if (expression) {

        Multiple-statements

     } else {

        Multiple-statements

           }

6. Printing of while loops.   Use one of the following

     while (expression) statement

     while (expression)

        Single-statement

     while (expression) {

        Multiple-statements

     }

7. For loops.   The bodies of all for-loops must be indented at least 3 (but no more than 5) spaces:

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

       cout << "Enter the number:" << flush;

       cin >> num;

       if (num < 0)      num_neg++;

       else if (num > 0) num_pos++;

       else              num_zero++;

    }

8. Methods.   The bodies of all functions must be indented at least 3 (but no more than 5) spaces:

double CircleArea(double r) {

         const double Pi = 3.1415;

           return Pi * r * r;

      }   

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 <iomanip>
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};
int smallest= times[0];
int second = times[0];
for (int i=0; i<=numRunners-1; i++)
{
if (times[i]<=smallest && i!=numRunners-1)
{
smallest=times[i];

}
else if (times[i]<= smallest && i==numRunners-1)
{
smallest=times[i];
cout << names[i] << ": " << smallest[i];
}
  
}
for (i = 0; i < numRunners ; i ++)
{
/* If current element is smaller than first then update both
first and second */
if (times[i] < smallest)
{
second = smallest;
smallest = times[i];
}

/* If times[i] is in between first and second then update second */
else if (times[i] < second && times[i] != smallest)
second = times[i];
}
if (second == numRunners)
cout << "There is no second smallest element ";
else
  
cout << "first runner is" names[i] << ": " << smallest[i] : "2nd runner is "names[i] << second[i];
}

return 0;
}