Thread: Sorting Program... :(

  1. #1
    Registered User
    Join Date
    Jan 2003

    Sorting Program... :(

    Hello everyone. I'm having a very hard time with a program I have. I have a list of players and their stats, and basically I ahve to sort them in all sorts of ways.

    "Write a program that reads the information for each player from a data file. The output should include

    an alphabetized list of names together with other pertinent information
    a list sorted according to scoring average
    an alphabetized list of all players with a real grade-point average with a 3.0 or higher."

    I am very much stuck on how to sort them. can anyone help me? I suppose i gotta use selection sort or something, but i have no idea how to go about it. i have my code.... i'll paste it if its lawful. Thank you people... I very much appreciate the help. Ask me if you would like to see my code so far.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Phildelphia, PA
    Not only is it lawful but it is preferred that you paste your code. How else can we help you?

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Jan 2003
    // Azhar Sheraze
    // 2nd 6 weeks program
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <fstream>
    #include <conio>
    // This is my struct. This is my user defined data type, consisting of strings
    // and float data types
    struct team
    	string name[2], 		   // name of player
       		 position,     // Position of player on basketball team
              school,       // School that Player graduated from
              height;       // Height of player
       float score_avg,     // Average Points player makes per game
       		rebound_avg,   // Rebound average player makes per game
             grade_avg,     // Player's GPA
             seasons_left;  // Seasons left for player to play
    void main()
      team           player[8];  // data type, Number of players
      ifstream       to_screen; // inputs from file to dos
      char temp; // This stores the value of a blank line, so i can obtain next players
    				   // information
      int count;
    "basketball.txt"); // This opens the text file "basketball.txt" so i
    										    // input info into my program
    	for(int a=0; a<8; a++)
          to_screen >> player[a].name[0];
          to_screen >> player[a].name[1];
          //	cout << player[a].name << "  ";
          getline(to_screen, player[a].position);
          //   cout << player[a].position << "  ";
          getline(to_screen, player[a].school);
          //   cout << player[a].school << "  ";
          getline(to_screen, player[a].height);
          //	cout << player[a].height << "  ";
    		to_screen >> player[a].score_avg;
          //	cout << player[a].score_avg << "  ";
          to_screen >> player[a].rebound_avg;
          //	cout << player[a].rebound_avg << "  ";
          to_screen >> player[a].grade_avg;
          //	cout << player[a].grade_avg << "  ";
          to_screen >> player[a].seasons_left;
          //	cout << player[a].seasons_left << "  " << endl << endl;
    // Output
    // Alpha List of Names by First .... just trying stuff
    temp = 'A';
    low = player[a].name[0][0];
    		for(int a=0; a<8; a++)
          if(temp= player[a].name[0][0];
          	if(player[a].name[0][0] ==

  4. #4
    Registered User
    Join Date
    Jan 2003
    Thanks a lot

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Seattle, WA
    You should be able to alphabatize by checking to see if the letter is greater (close to the end of alphabet) or less than (close to the beginning) since letters have numeric values. Do you know bubble sort? You should be able to figure something out using some sort of bubble sort
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."

  6. #6
    Registered User
    Join Date
    Jan 2003
    I was begining to see how to do alphabatize it like that (sorta...) but i realized some players at the same letter in their name, so I have no idea how to do that. the file looks like this:

    if you could maybe code it for me simply... then i could probably use that for all my other sorting stuff.

    Sooo confused

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    If you store your different player structs in an STL container such as a list or a vector you can then use the sort function on that container and automatically sort them based on whatever criteria you want. For instance to sort in ascending order by score_avg you could do:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    using namespace std;  // Edit: Forgot this
    // This is a simplified struct just to illustrate my point...
    struct team
        float score_avg;     // Average Points player makes per game
    // Define a function to compare the score_avg data members of two
    // different team structures.  You will need to create two additional
    // comparison functions to compare based on the name and the
    // grade point average.
    bool score_comp( const team& t1, const team& t2)
        return t1.score_avg < t2.score_avg;
    int main()
        vector<team> players;    // Hold a whole mess of team structures
        vector<team>::iterator it;
        team player;    // Hold a single players data
        // Add some team structs to the vector.  You will need to replace
        // this with your code to open your file and read all the data into
        // your team struct and push it into the vector.
        player.score_avg = 54.8;
        players.push_back(player);    // Players contains 1 team struct w/ value 54.8
        player.score_avg = 47.2;
        players.push_back(player);    // Players contains 2 team structs w/ values 54.8, 47.2
        player.score_avg = 99.8;
        players.push_back(player);    // Players contains 3 team structs w/ values 54.8, 47.2, 99.8
        // Now display the unsorted vector...
        // This will output 54.8, 47.2, 99.8 on individual lines
        for( it = players.begin(); it != players.end(); ++it )
            cout << it->score_avg << endl;
        // Now sort the vector using the score_comp function
        // Replace score_comp with name of your other comparison
        // functions to sort the data in players vector differently
        // based on your needs.
        // Now display the vector again, should be sorted...
        // This will output 47.2, 54.8, 99.8 on individual lines
        for( it = players.begin(); it != players.end(); ++it )
            cout << it->score_avg << endl;
        return 0;
    This will give you an idea of how easy it can be to sort data once it is in a vector. You can sort in reverse order based on the score_avg data member just by changing the '<' to '>' in the score_comp function.
    Last edited by hk_mp5kpdw; 11-05-2003 at 07:23 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM