CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    5

    Question How to determine ranks

    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");

    //Define variables
    const int SIZE = 21;
    char name1[SIZE], name2[SIZE], name3[SIZE], name4[SIZE], name5[SIZE];
    double time1, time2, time3, time4, time5;

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

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to determine ranks

    Code:
    char name1[SIZE], name2[SIZE], name3[SIZE], name4[SIZE], name5[SIZE];
    double time1, time2, time3, time4, time5;
    Turn these into an array and you can transform your code by using loops.

  3. #3
    Join Date
    Mar 2009
    Posts
    5

    Re: How to determine ranks

    Ok, but how do I Input the names of each of 5 runners and their respective times.
    This is what I tried:

    const int runners = 5;
    char name1[21], name2[21], name3[21], name4[21], name5[21];
    double time[runners];
    int rank;

    //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>>name1[21];
    cout<<endl; //For line spacing


    cout<<"2. Enter the name of the next runner"<<endl;
    cin>>name2[21];
    cout<<endl; //For line spacing

    cout<<"3. Enter the name of the next runner"<<endl;
    cin>>name3[21];
    cout<<endl; //For line spacing

    cout<<"4. Enter the name of the next runner"<<endl;
    cin>>name4[21];
    cout<<endl; //For line spacing

    cout<<"5. Enter the name of the next runner"<<endl;
    cin>>name5[21];
    cout<<endl; //For line spacing

    I uploaded an example of what my output file is suppose to look like and the screenshot of the program after I type in the first name and hit enter.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by tejen; March 23rd, 2009 at 10:03 PM.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to determine ranks

    This is not correct.
    Code:
    cout<<"1. Enter the name of the runner"<<endl;
    cin>>name1[21];
    cout<<endl; //For line spacing
    You probably mean:
    Code:
    cout<<"1. Enter the name of the runner"<<endl;
    cin>>name1;
    cout<<endl; //For line spacing
    However, get rid of this char arrays and use std::string.
    http://www.codeguru.com/cpp/cpp/stri...cle.php/c13267
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Mar 2009
    Posts
    5

    Question Re: How to determine ranks

    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");

    //Define variables
    const char runners = 100;
    char names[100][runners];


    //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;

    cout<<setprecision(2)<<fixed;
    cout<<left<<setw(20)<<names[0]<<" -------- "<<right<<setw(6)<<time[0]<<endl;
    cout<<left<<setw(20)<<names[1]<<" -------- "<<right<<setw(6)<<time[1]<<endl;
    cout<<left<<setw(20)<<names[2]<<" -------- "<<right<<setw(6)<<time[2]<<endl;
    cout<<left<<setw(20)<<names[3]<<" -------- "<<right<<setw(6)<<time[3]<<endl;
    cout<<left<<setw(20)<<names[4]<<" -------- "<<right<<setw(6)<<time[4]<<endl;

    cout<<endl;

    cout<<"Open the output file to view the results and the ranks of the all the runners"<<endl;


    system("pause"); //Pause the program so that the user can view the results



    return 0;

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to determine ranks

    I'm not very familiar of using loops for doing this. again your help is appreciated.

    Code:
    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;
    Code:
    long lCnt;
    
    cout<<"The names you entered are:"<<endl;
    for (lCnt = 0; lCnt < 5; lCnt++)
    {
       cout<<names[lCnt]<<endl;
    }
    cout<<endl;

  7. #7
    Join Date
    Mar 2009
    Posts
    5

    Re: How to determine ranks

    Quote Originally Posted by Skizmo View Post
    Code:
    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;
    Code:
    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.

  8. #8
    Join Date
    Mar 2009
    Posts
    4

    Re: How to determine ranks

    Quote Originally Posted by tejen View Post
    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.

  9. #9
    Join Date
    Mar 2009
    Posts
    5

    Re: How to determine ranks

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured