C++ PROGRAM Running the Race (class runner) Write a program that asks for the na
ID: 2246794 • Letter: C
Question
C++ PROGRAM
Running the Race (class runner)
Write a program that asks for the names of five runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place
Input Validation: Only accept positive numbers for the times.
The program should include:
-Constructor (default, copy constructor, with parameters, default arguments
-In the main program:
-Define a dynamic array composed of runner instances, where the number of runners is given by the user.
- The program must contain functions of:
-To Enter the information of the runners and their respective times. -
-Function:to Prints the first three positions.
-Function: to sort by the parameter of the times in the runners in ascending order.
- Validate your program with five runners.
- Implement the program Using Dynamic Memory in the name of the runner
-Implement other program using a string type variable in the name of the runner
Explanation / Answer
#include<iostream>
using namespace std;
class runner{
private:
string* names;
double* time;
int count;
public:
void acceptRunners() {
cout << "Enter number of runners: ";
cin >> count;
names = new string[count];
time = new double[count];
for (int i = 0; i < count; i++) {
cout << "Enter name of player " << i+1 << ": ";
cin >> names[i];
do {
cout << "Enter time of player " << i+1 << ": ";
cin >> time[i];
} while(time[i] < 0);
}
}
void sort() {
for(int i = 0; i < count -1; i++)
for(int j = 0; j < count - i - 1; j++)
if (time[j] > time[j+1]) {
double tmp_time = time[j];
time[j] = time[j+1];
time[j+1] = tmp_time;
string tmp_name = names[j]; names[j] = names[j+1];
names[j+1]= tmp_name;
}
}
void printWinners() {
for (int i = 0; i < (count < 3? count:3); i++) {
cout << "Rank "<< (i+1) << ": "
<< names[i] << " time: " << time[i] << endl;
}
}
};
int main() {
runner r;
r.acceptRunners();
r.sort();
r.printWinners();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.