Question

Write a c++ program as follows. Print the list of runners and times as example below....

Write a c++ program as follows. Print the list of runners and times as example below. 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.

This is what I have so far.

#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];

Example:

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

// function declaration
int checkRunner(int array[], int times[],string names[]);
int secondRunner(int times[]);

int main ()
{
   // local variable declaration:
   const int numRunners = 16;  
   int array[numRunners];
   for(int i=1;i<=numRunners;i++)
   array[i]=i;
    string names[] ={"Elena", "Thomas", "Hamilton", "Suzie", "Phil","Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel","Neda","Aaron", "Kate"};
    int times[] ={341, 873, 278, 329, 445, 402, 388, 275, 243, 334, 412,393, 299,343, 317, 220};
    for (int i = 0; i < numRunners; i++) {
        cout << names[i] << ": " << times[i]<<"\n";
    }

   // calling a function to get max value.
   int ret = checkRunner(array,times,names);

   cout << "Fastest runner index value : " << ret << endl;

   return 0;
}

// function returning the max between two numbers
int checkRunner(int array[], int times[],string names[])
{
int index=0,max=0,fastrunner=0,minIndex=0;
for(int i=0;i<sizeof(array);i++){
      if(times[i]>max){
          max=times[i];
          index=i+1;
      }
      if(fastrunner<times[i]){
          fastrunner = times[i];
          minIndex=i;
      }
}
cout<<"fastest runner is :"<<names[minIndex]<<"\n";
int secondWinner = secondRunner(times);
for(int i=0;i<sizeof(array);i++){
      if(times[i]==secondWinner){
          cout<<"second winner is:"<<names[i]<<"\n";
      }
}
return index;
}
int secondRunner(int times[]){
    int min,smin,i;
    smin = times[0];
    for (i = 0; i <= sizeof(times); i++){
       if (times[i] < min){
          smin=min;
          min=times[i];
       }
       if(min<times[i] && times[i]<smin){
           smin=times[i];
       }
    }
    return smin;
}

Add a comment
Know the answer?
Add Answer to:
Write a c++ program as follows. Print the list of runners and times as example below....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FastestRunner.cpp A group of students decided to run in the Columbus Marathon. Their names and times...

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

  • need help to complete this java program // add appropriate import statements here. // These imports...

    need help to complete this java program // add appropriate import statements here. // These imports you can leave as is. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import javafx.stage.Stage; /** @author yourAccountNameHere */ public class ConnectTheDots extends Application {            /*     * Do not add code to main(). Add it below in connectTheDots instead.     */     public static void main(String[] args) {         launch(args);     }         /*...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT