Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 2 Weeks Ago   #1 (permalink)
New to Overclock.net
 
Join Date: Nov 2009
Posts: 2

Rep: 0 noseaboutit Unknown
Unique Rep: 0
Trader Rating: 0
Default need help with Sorting program

I'm having trouble with the sorting function in this program.
i seem to be able to sort three players but i can't get the others to sort


here are the instructions for the program along with what i have written


For this assignment, you are to write a program that will process a data set that represents the players that were with the Chicago Blackhawks during the 2008 - 2009 regular season. The information in the file will be needed for later processing, so it will be stored in a set of arrays that will be displayed, sorted, and displayed (again).

For the assignment, you will need to declare four arrays, each of which will hold a maximum of 30 elements:

one array of integers to hold the goals
one array of integers to hold the assists
one array of integers to hold the attempted shots
one array of strings to hold the player names
The four arrays will be parallel arrays. This means that the information for one player can be found in the same "spot" in each array. This also means that any change that is applied to one array must also be applied to the other three in order to maintain data integrity.

The array of strings should be declared (and initialized as follows):

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" };
The name of the string array and the name of the symbolic constant (to be discussed later) may be changed, but the initial values MUST remain the same.

Suggested Logic for main():

NOTE: all of the functions mentioned in this logic are described below.

Fill the three integer arrays by calling the buildArrays() function.

Display the four arrays by calling the printArrays() function.

Sort the four arrays by calling the sortArrays() function.

Display the sorted arrays by calling the printArrays() function.

Input

The input for this program will be read from a disk file named players.txt. The file consists of a set of records for the players that were with the Chicago Blackhawks during the 2008 - 2009 season. Each record represents one player and is made up of three values: the first is the number of goals the player scored, the second is the number of assists the player recorded, and the third is the number of shots the player attempted. The file resembles the following:

29 48 249
25 45 254
34 35 195
22 31 139
7 45 108
NOTE: You may assume that if there is a number of goals in record, then there will be a number of assists and number of shots.

Functions to write and use

int buildArrays( int goals[], int assists[], int shots[] )

This function will read the file of data and fill the three arrays. It takes as its arguments the three integer arrays. It returns the number of valid players.

Suggested logic:

Declare an input file stream (see "buildArrays notes" below) and any other variables that
you may need

Open the input file and make sure that it opened correctly (see "buildArrays notes" below)

Initialize a subscript to the beginning of the array

Get the first number of goals from input (see "buildArrays notes" below)

While there are records in the file (see "buildArrays notes" below)

Put the number of goals into the appropriate array

Get a number of assists from input
Put the number of assists into the appropriate array

Get a number of shots attempted from input
Put the number of shots attempted into the appropriate array

Increment the subscript to the next spot in the array

Get the next number of goals from input
Endwhile

Close the input file (see "buildArrays notes" below)

Return the number of players in an array (think about the subscript)
buildArrays notes:

The data is being read from an input file. Before the file can be processed, you must:

declare an input file stream (see "Programming Note 1" below)

ifstream inFile;
open the file and make sure that it opened correctly

inFile.open( "averages.txt" );

if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
The above code will open the file and then make sure that the file did indeed open correctly. Note the TWO backslashes!! You must code TWO for every ONE in the path of the file.

To read from a file, use the name of the ifstream in place of cin. For example:

int num;

inFile >> num;

To test if there are records in the file, simply use the name of the input file stream as a boolean variable. For example:

while ( inFile )
As long as there are records in the file, the name of the input file stream name will resolve to true. Once all of the records have been read from the file, the input file stream name will resolve to false.

Once a value has been read from a file, it can be put into the appropriate array element:

someArray[i] = num;
After all of the data has been read from the file, the file must be closed. To do this, execute the following:

inFile.close();
void printArrays( string names[], int goals[], int assists[], int shots[], int numPlayers )

This function will display the information for the Chicago Blackhawks players. For each player, display their name, number of goals scored, number of assists recorded, number of points, number of shots, and shooting percentage.

This function takes as its arguments the four arrays and the number of elements in the arrays.

Use the following formulas for the calculated values:

Number of Points = Number of goals scored + Number of assists

Shooting Percentage = Number of Goals Scored / Number of shots taken * 100
Use the following as a basic format for the output:

Chicago Blackhawks 2008-2009 Player Stats

Player Goals Assists Points Shots Shooting %
------------------------------------------------------------------
Martin Havlat 29 48 77 249 11.6
Patrick Kane 25 45 70 254 9.8
Jonathan Toews 34 35 69 195 17.4
Kris Versteeg 22 31 53 139 15.8
Brian Campbell 7 45 52 108 6.5
Andrew Ladd 15 34 49 195 7.7
NOTE 1: It's possible that a player didn't attempt any shots during the season. If that's the case, the shooting percentage should be 0.0%.

NOTE 2: The player names should be left justified. All of the numeric values should be right justified. The shooting percentage should be displayed with exactly 1 digit after the decimal point.

void sortArrays( string names[], int goals[], int assists[], int shots[], int numPlayers )

This function will sort the arrays in ASCENDING order based on the player names. Use the selection sort algorithm presented in lecture.

This function takes as its arguments the four arrays and the number of elements in the arrays.

It's important to note that the four arrays are parallel arrays, meaning that elements in each array that have the same subscript correspond. Therefore, every time the algorithm swaps two elements in the player names array, it must also swap the corresponding elements in the goals, assists and shots arrays.

Programming Notes:

Add #include <fstream> at the top of your program.

Each array should be able to hold 30 elements. Use a symbolic constant to represent the maximum size of an array (NUM_PLAY in the example above).

Each array has the capability to hold 30 elements, however, that does not mean that they will all be used. This is the reason that the number of elements in the array is being passed to the sort and print functions. This value is the return value from buildArrays.

Copy the input file to your hard disk and write your program so that it reads the data from the current directory (ie. don't specify a file path).

Hand in a copy of your source code using Blackboard.





here is what i have from the program already done





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

//Define a Constant

#define NUM_PLAY 30

using namespace std;

//Declaring my arrays

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


//Main Program
int main()
{
int numPlayers = 0;
int goals[NUM_PLAY];
int assists[NUM_PLAY];
int shots[NUM_PLAY];


//inputing List of players
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);

//displays Stats before sort
printArrays(players, goals, assists, shots, numPlayers);

cout << endl;

sortArrays(players, goals, assists, shots, numPlayers);

//displays Stats after sort

printArrays(players, goals, assists, shots, numPlayers);



return 0;
}



//Writing Buildarray funtion
int buildArrays(int goals[NUM_PLAY], int assists[NUM_PLAY], int shots[NUM_PLAY])
{


ifstream inFile;
int num;
int i;


//calling file from hard disk
inFile.open("players.txt");

if ( inFile.fail() )

{
cout << "input file did not open";

exit(0);

}


//inputs data from file into arrays
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;
}


//writing printarray function

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



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


//Loop to display data from file as well as shot perc and points

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

}



//Writing sortarrays funtion
void sortArrays(string players[], int goals[], int assists[], int shots[], int numPlayers)
{
int top;
int ssf;
int worksub;
int temp;

string tempname;
for(top = 0; top < numPlayers; top++)
{
ssf=top;
for(worksub = top+1; worksub < numPlayers; worksub++)
{
if( players[worksub] < players[ssf])
ssf = worksub;

}

tempname = players[top];
players[top] = players[ssf];
players[ssf] = temp;

tempname = goals[top];
goals[top] = goals[ssf];
goals[ssf] = temp;

tempname = assists[top];
assists[top] = assists[ssf];
assists[ssf] = temp;

tempname = shots[top];
shots[top] = shots[ssf];
shots[ssf] = temp;


}
}




thank you for the help!!!!!!
noseaboutit is offline   Reply With Quote
Old 2 Weeks Ago   #2 (permalink)
New to Overclock.net
 
Join Date: Nov 2009
Posts: 2

Rep: 0 noseaboutit Unknown
Unique Rep: 0
Trader Rating: 0
Exclamation i Figured it out but having trouble with another portion of the program

i figured out the first part of the program but im having trouble creating 2 more functions that are needed in the program. if you could help me out that would be great ill send the requirements and what i have so far..



For this assignment, add a couple of functions to go along with the ones that were coded for Program 7. The program will continue to process a data set that represents the players that were with the Chicago Blackhawks during the 2008 - 2009 regular season. The information in the file will still be stored in a set of arrays. The arrays will be sorted, displayed, and then processed.

The new code for the assignment will involve:

writing a function to search for a specific player

writing a function that will find the most number of goals scored, assists, and number of shots attempted over the season. It will use the call-by-reference techniques that were discussed in lecture.

Suggested Logic for main():

NOTE: all of the functions mentioned in this logic are described below.

Fill the three integer arrays by calling the buildArrays() function.

Sort the four arrays by calling the sortArrays() function.

Display the sorted arrays by calling the printArrays() function.

Search for "Dustin Byfuglien" by calling the playerSearch() function.

If the player is found, display the number of goals and assists for that player. If the player is not found, display a message, including the player's name, indicating that the player was not found.

Search for your name by calling the playerSearch() function.

If the player (you) is found, display the number of goals and assists for that player. If the player is not found, display a message, including the player's name, indicating that the player was not found.

Call the findMost() function to determine the most goals, assists, and number of shots.

Display the name of the player that scored the most goals, include the number of goals in parenthesis. Display the name of the player that had the most assists, include the number of assists in parenthesis. Display the name of the player that attempted the most shots, include the number of shots in parenthesis.

Functions to write and use

bool playerSearch( string names[], int numPlayers, string nameToFind, int &nameLoc )

This function will sequentially search the array of strings for a specific name. If the name is found, the "location" (subscript) will be "passed back" using the fourth argument and true will be returned. If the name is not found, return false.

This function takes four arguments: an array of strings to be searched, the number of players in the array, the name to search for, and an integer reference to pass back where the name was found.
void findMost( int goals[], int assists[], int shots[], int numPlayers, int &mostGoalsLoc, int &mostAssistsLoc, int &mostShotsLoc )

This function will search the appropriate array to find the most goals scored, assists, and attempted shots. It should pass back the "location" (subscript) of each value using the last three arguments.

This function takes as its arguments the three integer arrays, the number players in the array, and three integer references to pass back the results.

Output:

Chicago Blackhawks 2008-2009 Player Stats

Player Goals Assists Points Shots Shooting %
------------------------------------------------------------------
Aaron Johnson 3 5 8 27 11.1
Adam Burish 6 3 9 83 7.2
Andrew Ladd 15 34 49 195 7.7
Ben Eager 11 4 15 80 13.8
Brent Seabrook 8 18 26 132 6.1
Brent Sopel 1 1 2 15 6.7
Brian Campbell 7 45 52 108 6.5
Cam Barker 6 34 40 101 5.9
Colin Fraser 6 11 17 67 9.0
Dave Bolland 19 28 47 111 17.1
Duncan Keith 8 36 44 173 4.6
Dustin Byfuglien 15 16 31 202 7.4
Jack Skille 1 0 1 14 7.1
Jacob Dowell 0 0 0 0 0.0
Jonathan Toews 34 35 69 195 17.4
Jordan Hendry 0 0 0 1 0.0
Kris Versteeg 22 31 53 139 15.8
Martin Havlat 29 48 77 249 11.6
Matt Walker 1 13 14 83 1.2
Niklas Hjalmarsson 1 2 3 15 6.7
Pascal Pelletier 0 0 0 7 0.0
Patrick Kane 25 45 70 254 9.8
Patrick Sharp 26 18 44 184 14.1
Sammy Pahlsson 7 11 18 88 8.0
Tim Brent 0 0 0 0 0.0
Troy Brouwer 10 16 26 126 7.9


Dustin Byfuglien scored 15 goals and had 16 assists.

Amy Byrnes was not found


Player with the most GOALS: Jonathan Toews (34)
ASSISTS: Martin Havlat (48)
SHOTS: Patrick Kane (254)
Programming Notes:

Add #include <fstream> at the top of your program.

Each array should be able to hold 30 elements. Use a symbolic constant to represent the maximum size of an array (NUM_PLAY in the example above).

Each array has the capability to hold 30 elements, however, that does not mean that they will all be used. This is the reason that the number of elements in the array is being passed to the sort and print functions. This value is the return value from buildArrays.

Copy the input file to your hard disk and write your program so that it reads the data from the current directory (ie. don't specify a file path).

The new output for this program (players that are found/not found and statistics should all be printed in main - NOT in the playerSearch or findMost functions.





this is what have done




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

//Define a Constant

#define NUM_PLAY 30

using namespace std;

//Declaring my arrays

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

//Declaring new arrays

bool playerSearch(string[], int, string, int);
void findMost(int[], int[], int[], int, int, int, int);


//Main Program
int main()
{
int numPlayers = 0;
int goals[NUM_PLAY];
int assists[NUM_PLAY];
int shots[NUM_PLAY];



//inputing List of players
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);

//displays Stats after sort

printArrays(players, goals, assists, shots, numPlayers);



return 0;
}



//Writing Buildarray funtion
int buildArrays(int goals[NUM_PLAY], int assists[NUM_PLAY], int shots[NUM_PLAY])
{


ifstream inFile;
int num;
int i;


//calling file from hard disk
inFile.open("players.txt");

if ( inFile.fail() )

{
cout << "input file did not open";

exit(0);

}


//inputs data from file into arrays
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;
}


//writing printarray function

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



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


//Loop to display data from file as well as shot perc and points

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

}



//Writing sortarrays funtion
void sortArrays(string players[], int goals[], int assists[], int shots[], int numPlayers)
{
int top;
int ssf;
int worksub;
int temp;

string tempname;
for(top = 0; top < numPlayers; top++)
{
ssf=top;
for(worksub = top+1; worksub < numPlayers; worksub++)
{
if( players[worksub] < players[ssf])
ssf = worksub;

}

tempname = players[top];
players[top] = players[ssf];
players[ssf] = tempname;

temp = goals[top];
goals[top] = goals[ssf];
goals[ssf] = temp;

temp = assists[top];
assists[top] = assists[ssf];
assists[ssf] = temp;

temp = shots[top];
shots[top] = shots[ssf];
shots[ssf] = temp;


}
}


//Writing playerSearch funtion
bool playerSearch( string names[], int numPlayers, string nameToFind, int &nameLoc )
noseaboutit is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 12:42 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.09763 seconds with 8 queries