-
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.
-
Not only is it lawful but it is preferred that you paste your code. How else can we help you?
-
Code:
// 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;
to_screen.open("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';
count=0;
do
{
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] ==
}
temp++;
a=0;
}
}while(count!=8);
}
-
-
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
-
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:
http://cphs.leander.isd.tenet.edu/te...basketball.txt
if you could maybe code it for me simply... then i could probably use that for all my other sorting stuff.
Sooo confused :(
-
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:
Code:
#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.
sort(players.begin(),players.end(),score_comp);
// 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.