1. 300 runners took part in the Spar Women’s Race. Their times in minutes are
stored in one array, their competition number in a second parallel array and
their names and initials in a third parallel array. Using a bubble sort, sort these
arrays according to their times, with the fastest time first, and the slowest last.
Remember to swap the names in the names array and the competition number
in the compNum array along with the values in the time array. Then display the
competition number and the name of the fastest 3 athletes.
#include<iostream>
#include<string>
using namespace std;
int main ()
{
int numberRunners=300;
//Their times in minutes are stored in one array
int times[300];
//their competition number in a second parallel array
int compNum[300];
//and their names and initials in a third parallel array
string names[300];
for(int i = 0; i<numberRunners; i++) {
cout<<"Enter name of the athlete : ";
getline(cin,names[i]);
cout<<"Enter competition number of the athlete: ";
cin>>compNum[i];
cout<<"Enter time of the athlete ";
cin>>times[i];
fflush(stdin);
cout<<"\n";
}
//sing a bubble sort, sort these arrays according to their times,
//with the fastest time first, and the slowest last.
for(int i = 0; i<numberRunners; i++) {
for(int j = i+1; j<numberRunners; j++)
{
if(times[j] < times[i]) {
int tempTime = times[i];
times[i] = times[j];
times[j] = tempTime;
//to swap the names in the names array and the competition number
//in the compNum array along with the values in the time array.
int tempCompNum = compNum[i];
compNum[i] = compNum[j];
compNum[j] = tempCompNum;
string tempName= names[i];
names[i] = names[j];
names[j] = tempName;
}
}
}
//Then display the competition number and the name of the fastest 3 athletes.
cout <<"\nThe fastest 3 athletes:\n";
for(int i = 0; i<3; i++) {
cout<<"The competition number: "<<compNum[i]<<"\n";
cout<<"The name of the athlet: "<<names[i]<<"\n";
cout<<"The time of the athlet: "<<times[i]<<" minutes\n\n";
}
system("pause");
return 0;
}
Comments
Leave a comment