Write a c++ program as follows. Print the list of runners and times as example b
ID: 645102 • Letter: W
Question
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
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>
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]<<" ";
}
// 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]<<" ";
int secondWinner = secondRunner(times);
for(int i=0;i<sizeof(array);i++){
if(times[i]==secondWinner){
cout<<"second winner is:"<<names[i]<<" ";
}
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.