I have an assignment where i have to to determine ranks of 5 runners according to their times that were input. The only way of doing this that I know is by using if and else if statements. and I think there are over 120 permuations for 12345 so how do i determine the ranks of 5 people when there are over 120 permuations? Does C++ have way of making this easier. Im very new to this and any help would be appreciated. Thank you!
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ofstream outputfile;
outputfile.open("c:\\Project_2\\M20349431_output_2.txt");
ifstream infile;
infile.open("c:\\Project_2\\M20349431_output_2.txt");
//Display the purpose oof this program
cout<<"This program will determine the ranks of 5 runners according to their time."<<endl;
cout<<endl;
//Request the names and times of each runner.
cout<<"1. Enter the name of the runner"<<endl;
cin.getline(name1, SIZE);
cout<<endl; //For line spacing
cout<<"2. Enter the name of the next runner"<<endl;
cin.getline(name2, SIZE);
cout<<endl; //For line spacing
cout<<"3. Enter the name of the next runner"<<endl;
cin.getline(name3, SIZE);
cout<<endl; //For line spacing
cout<<"4. Enter the name of the next runner"<<endl;
cin.getline(name4, SIZE);
cout<<endl; //For line spacing
cout<<"5. Enter the name of the next runner"<<endl;
cin.getline(name5, SIZE);
cout<<endl; //For line spacing
//Request the time of each runner
cout<<"Enter the time for "<<name1<<endl;
cin>>time1;
if(time1 <= 0) //Check for negative numbers or 0)
{ cout<<"Error! Invalid number. You must enter a number greater than 0"<<endl; //Display "Error" message if a invalid number is entered
cin>>time1;
}
cout<<endl; //For line spacing
cout<<"Enter the time for "<<name2<<endl;
cin>>time2;
if(time2 <= 0) //Check for negative numbers or 0)
{
cout<<"Error! Invalid number. You must enter a number greater than 0"<<endl; //Display "Error" message if a invalid number is entered
cin>>time2;
}
cout<<endl; //For line spacing
cout<<"Enter the time for "<<name3<<endl;
cin>>time3;
if(time3 <= 0) //Check for negative numbers or 0)
{
cout<<"Error! Invalid number. You must enter a number greater than 0"<<endl; //Display "Error" message if a invalid number is entered
cin>>time3;
}
cout<<endl; //For line spacing
cout<<"Enter the time for "<<name4<<endl;
cin>>time4;
if(time1 <= 0) //Check for negative numbers or 0)
{ cout<<"Error! Invalid number. You must enter a number greater than 0"<<endl; //Display "Error" message if a invalid number is entered
cin>>time4;
}
cout<<endl; //For line spacing
cout<<"Enter the time for "<<name5<<endl;
cin>>time5;
if(time1 <= 0) //Check for negative numbers or 0)
{ cout<<"Error! Invalid number. You must enter a number greater than 0"<<endl; //Display "Error" message if a invalid number is entered
cin>>time5;
}
cout<<endl; //For line spacing
//Determine the rank of each runner according to the times that were entered
if(time1 < time2 && time2 < time3 && time3 < time4 && time4 < time5)
{
outputfile<<"\nThe ranks of the runners according to their times\n";
outputfile<<"---------------------------------------------------\n";
outputfile<<"Rank 1 is "<<setw(25)<<name1<<" With the time of: "<<setw(6)<<time1<<endl;
outputfile<<"Rank 2 is "<<setw(25)<<name2<<" With the time of: "<<setw(6)<<time2<<endl;
outputfile<<"Rank 3 is "<<setw(25)<<name3<<" With the time of: "<<setw(6)<<time3<<endl;
outputfile<<"Rank 4 is "<<setw(25)<<name4<<" With the time of: "<<setw(6)<<time4<<endl;
outputfile<<"Rank 5 is "<<setw(25)<<name5<<" With the time of: "<<setw(6)<<time5<<endl;
}
else if(time5 < time4 && time4 < time3 && time3 < time2 && time2 < time1)
{
outputfile<<"\nThe ranks of the runners according to their times\n";
outputfile<<"---------------------------------------------------\n";
outputfile<<"Rank 1 is "<<setw(25)<<name5<<" With the time of: "<<setw(6) <<time5<<endl;
outputfile<<"Rank 2 is "<<setw(25)<<name4<<" With the time of: "<<setw(6)<<time4<<endl;
outputfile<<"Rank 3 is "<<setw(25)<<name3<<" With the time of: "<<setw(6)<<time3<<endl;
outputfile<<"Rank 4 is "<<setw(25)<<name2<<" With the time of: "<<setw(6)<<time2<<endl;
outputfile<<"Rank 5 is "<<setw(25)<<name1<<" With the time of: "<<setw(6)<<time1<<endl;
}
and so on....... But then I realized there are about 120 permuations for 12345 and i would have to repeat this process over 120 times Is their an easier way to determine ranks, or do i have to do this 120 times. Please help
Thanks for the tips. I changed my entire source code and used arrays instead, but I still don't know how to determine ranks for 5 people according to their lap times??? I'm not very familiar of using loops for doing this. again your help is appreciated.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ofstream outputfile;
outputfile.open("c:\\Project_2\\M20349431_output_2.txt");
ifstream infile;
infile.open("c:\\Project_2\\M20349431_output_2.txt");
//Display the purpose oof this program
cout<<"This program will determine the ranks of 5 runners according to their time."<<endl;
cout<<endl;
//Request the names of each runner.
cout<<"1. Enter the names of all the runners"<<endl;
cin>>names[0];
cout<<endl;
cin>>names[1];
cout<<endl;
cin>>names[2];
cout<<endl;
cin>>names[3];
cout<<endl;
cin>>names[4];
cout<<endl;
//Display the names
cout<<"The names you entered are:"<<endl;
cout<<names[0]<<endl;
cout<<names[1]<<endl;
cout<<names[2]<<endl;
cout<<names[3]<<endl;
cout<<names[4]<<endl;
cout<<endl;
//Request Time
cout<<"2. Enter the times for all the runners"<<endl;
cout<<endl;
const int times = 5;
double time[times];
cout<<"Enter the time for "<<names[0]<<endl;
cin>>time[0];
cout<<endl;
while(time[0] <= 0)
cout<<"Error! Time Must Be Greater Than Zero. Restart Program"<<endl;
cout<<"Enter the time for "<<names[1]<<endl;
cin>>time[1];
cout<<endl;
cout<<"Enter the time for "<<names[2]<<endl;
cin>>time[2];
cout<<endl;
cout<<"Enter the time for "<<names[3]<<endl;
cin>>time[3];
cout<<endl;
cout<<"Enter the time for "<<names[4]<<endl;
cin>>time[4];
cout<<endl;
//Display names and times together
cout<<"You entered:"<<endl;
cout<<endl;
long lCnt;
cout<<"The names you entered are:"<<endl;
for (lCnt = 0; lCnt < 5; lCnt++)
{
cout<<names[lCnt]<<endl;
}
cout<<endl;
This gives me the same output for the names, I need help determining rank 1, rank2, rank3, rank4 and rank5 according to the runners times. The name with the lowest time would be rank1 and so on.
This gives me the same output for the names, I need help determining rank 1, rank2, rank3, rank4 and rank5 according to the runners times. The name with the lowest time would be rank1 and so on.
If it were my program, I would create a struct called "Runner" or something along those lines. You could have a string for the runners name included, and then a float for their time. Create a vector of 5 runners. Then, using a sort (for this a bubble sort would suffice) you can reorder the vector in order of which time was the lowest, then the next, then the next, etc. Now, since the runners are completely reordered in the vector, you can use that for loop to output each of their names and times in correct order.
Thanks for the tip. I haven't gotten as far as learning about structs, vectors and sorts ( maybe thats why i'm having such a hard time trying to figure this out). But I will try what you suggested. again, Thank you.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.