confused with array sorting

Hi everyone, I am new here. I have a question becuase this program has been bugging me really bad. We are supposed to have this program build arrays according to the chicago blackhawks team and print out there goals, shots, assists, etc. and sort them also alphabetically. This is what I came up with but for some reason, when I try to print it out it takes a name out and duplicates one. Also when I try to organize the goals and other stats with the player my program crashes.



----------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>

#define NUM_PLAY 30

using namespace std;

int buildArrays(int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);
void sortArrays(string[], int[], int[], int[], int);

int main()
{
int numPlayers = 0;
int goals[NUM_PLAY];
int assists[NUM_PLAY];
int shots[NUM_PLAY];
string players[NUM_PLAY] = {"Martin Havlat", "Patrick Kane", "Jonathan Toews",
"Kris Versteeg", "Brian Campbell", "Andrew Ladd",
"Dave Bolland", "Patrick Sharp", "Duncan Keith",
"Cam Barker", "Dustin Byfuglien", "Brent Seabrook",
"Troy Brouwer", "Sammy Pahlsson", "Colin Fraser",
"Ben Eager", "Matt Walker", "Adam Burish",
"Aaron Johnson", "Niklas Hjalmarsson", "Brent Sopel",
"Jack Skille", "Jordan Hendry", "Pascal Pelletier",
"Tim Brent", "Jacob Dowell"};

numPlayers = buildArrays(goals, assists, shots);
sortArrays(players, goals, assists, shots, numPlayers);
printArrays(players, goals, assists, shots, numPlayers);

system ("pause");

return 0;
}
int buildArrays(int goals[NUM_PLAY], int assists[NUM_PLAY], int shots[NUM_PLAY])
{
ifstream inFile;
int num;
int i;

inFile.open("players.txt");

if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}

while (inFile)
{
for(i = 0; i < NUM_PLAY; i++)
{
inFile >> num;
goals[i] = num;
inFile >> num;
assists[i] = num;
inFile >> num;
shots[i] = num;
if(inFile.fail())
break;
}
}

return i;
}
void printArrays(string names[], int goals[], int assists[], int shots[], int numPlayers)
{
int i;
int points = 0;
float shotPercent = 0;

cout << fixed << showpoint << setprecision(1) << left;
cout << '\t' << "Chicago Blackhawks 2008-2009 Player Stats";
cout << endl;
cout << endl;
cout << setw(25) << "Players" << setw(10)<< "Goals" << setw(10) << "Assists" << setw(10) << "Points" << setw(10) << "Shots" << setw(8) << "Shooting %";
cout << endl;
cout << "---------------------------------------------------------------------------";
cout << endl;
for(i = 0; i < numPlayers; i++)
{
points = goals[i] + assists[i];
shotPercent = ((float) goals[i] / shots[i]) * 100;

if (shots[i] == 0)
{
cout << setw(25) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) << points << setw(10) << shots[i] << 0.0 << setw(8) << endl;
}
else
{
cout << setw(25) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) << points << setw(10) << shots[i] << shotPercent << setw(8) << endl;
}
}

}
void sortArrays(string names[], int goals[], int assists[], int shots[], int numPlayers)
{
int top;
int temp;
int ssf;
int ptr;

for(top = 0; top < numPlayers; top++)
{
for(ptr = top, ssf = top; ptr <= numPlayers; ptr++)
{
if(names[ptr] < names[ssf])
ssf = ptr;
}
names[temp] = names[top];
names[top] = names[ssf];
names[ssf] = names[temp];
}
}
-------------------------------------
What exactly are you asking for help with here? I understand you said what the program should do and what is going wrong, but at what point is that happening? I'd rather not look through the entire code as I am lazy, but I'm happy to help if you can provide a specific place or some output would be helpful as well :)
Oh my bad forgot to add that part. Well I believe its with the sortArrays function. Everything else seems to run fine. I had all the players added and all the stats with that player. Until I started to write the sortArrays function since I was writing that I got it like almost all the way sorted. I think theres something wrong in the second for loop, but i just cant figure it out.
Yea, your sortArrays looks like it wants to be a bubble sort, but as implemented its a bit more like
a babble sort.

For one thing, temp is never initialized in sortArrays.

For another, the "swap" portion of the bubble sort algorithm needs to be inside the inner for loop.
You've got it outside.

(I assume you also realize that you have to perform identical swaps of goals, assists, and shots.)
I tried what you said but still no success. It does the same thing.


void sortArrays(string names[], int goals[], int assists[], int shots[], int numPlayers)
{
int top;
int temp = 0;
int ssf;
int ptr;

for(top = 0; top < numPlayers; top++)
{
for(ptr = top, ssf = top; ptr <= numPlayers; ptr++)
{
if(names[ptr] < names[ssf])
{
ssf = ptr;
names[temp] = names[top];
names[top] = names[ssf];
names[ssf] = names[temp];
}
}
}
}
so, temp = 0 (in your last post)

which means that

names[temp] is the same as names[0] in the following code

names[0] was "Martin Havlat" to start with, but you replace it with whatever was in names[top].


1
2
3
4
5
6

ssf = ptr;
names[temp] = names[top];
names[top] = names[ssf];
names[ssf] = names[temp];


edit: try,


string temp; int itemp;

1
2
3
temp = names[b]; names[b] = names[a]; names[a] = temp;
itemp = goals[b]; goals[b] = goals[a]; goals[a] = itemp;
itemp = shots[b]; shots[b] = shots[a]; shots[a] = itemp;
Last edited on
Thank you so much totally forgot about declaring the temp as a string. Realized that I just mixed things up a bit. Thanks again everyone for helping.
Topic archived. No new replies allowed.