I have to "re-write" the program that I wrote before. Here is the prompt of what to do:
Overview
For this assignment, re-write program 7 so that rather than using multiple arrays to hold the player information, it uses a single array of structures.
The structure that will be used in this programs will represent one player. It has fields for the player name, number of goals scored by the player, and the number of assists for the player.
struct playerInfo
{
char playerName[25];
int goalsScored;
int assist;
};
For the assignment, you will only need to declare one array that can hold a maximum of 35 elements:
one array of playerInfo to hold all of the player information
Suggested Logic for main():
NOTE: all of the functions mentioned in this logic are described below.
Fill the array by calling the buildAr() function.
Display the title "Chicago Blackhawks UNSORTED Report"
Display the array by calling the printAr() function.
Sort the array by calling the sortAr() function.
Display the title "Chicago Blackhawks SORTED Report"
Display the sorted array by calling the printAr() function.
Calculate the average number of the goals and assists by calling the calcStats function
Display the average number of the goals and assists
--------------------------------------------------------------------------------
Input
The input for this program will be read from a disk file named binary_hockey.txt. The file consists of a set of playerInfo records. Each record represents one player that has been written in BINARY.
Here is the link for the binary_hockey.txt file---http://faculty.cs.niu.edu/~byrnes/csci240/pgms/binary_hockey.txt
--------------------------------------------------------------------------------
Functions to write and use
int buildAr( playerInfo players[] )
This function will read the file of data and fill the array. It takes as its argument the array of playerInfo structures. It returns the number of valid players that were placed in the arrays.
Suggested logic:
Declare an input file stream and any other variables that you may need
Open the input file in BINARY mode and make sure that it opened correctly (see "buildAr notes" below)
Initialize a subscript to the beginning of the array
Get the first player record from input by using the read function (see "buildAr notes" below)
While there are records in the file
Put the player record into the players array
Increment the subscript to the next spot in the array
Get the next player from input by using the read function
Endwhile
Close the input file
Return the number of players that were placed in the array (think about the subscript)
buildAr notes:
The information in the file has been written in BINARY rather than regular text like the previous assignment. This means that the statement to open the file must be altered to make sure that the data is read properly. To open a file in BINARY mode and make sure that it opened correctly:
inFile.open( "binary_hockey.txt", ios::binary );
if ( inFile.fail() )
{
cout << "The binary_hockey.txt input file did not open";
exit(-1);
}
Since the data contains binary data rather than text, the information has to be read in a slightly different manner. Rather than reading each field individually like in Program 7, each player's information will be read as a "chunk" of data by using the read function:
playerInfo player;
inFile.read( (char *) &player, sizeof(playerInfo) );
As in the previous assignment, to test if there are records in the file, simply use the name of the input file stream as a boolean variable. As long as there are records in the file, 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.
After all of the data has been read from the file, the file must be closed.
--------------------------------------------------------------------------------
void printAr( playerInfo players[], int numPlayers )
This function will display the information for the players. For each player, display the player name, number of goals scored, number of assists, and the number of points.
This function takes as its arguments the array of playerInfo structures and the number of players in the array.
The number of points is calculated as follows:
Points = number of goals + number of assists
Use the following as a basic format for the output:
Player Goals Assists Points
----------------------------------------------------
Jonathon_Toews 30 41 71
Patrick_Sharp 34 34 68
--------------------------------------------------------------------------------
void sortAr( playerInfo players[], int numPlayers )
This function will sort the arrays in ASCENDING order based on the player name. Use the selection sort algorithm presented in lecture.
This function takes as its arguments the array of playerInfo structures and the number of players in the array.
Don't forget that since the player name is stored as a real C++ string (null-terminated character array) rather than using the string data type, the comparison of player names MUST use the strcmp function instead of the < operator.
--------------------------------------------------------------------------------
void calcStats( playerInfo players[], int numPlayers, double &averageGoals, double &averageAssists )
This function will calculate the average number of the goals that were scored and assists.
This function takes as its arguments the array of playerInfo structures, the number of players in the array, a reference to a double that will be used to pass-back the average number of goals, a reference to a double that will be used to pass back the average number of assists.
--------------------------------------------------------------------------------
Programming Notes:
Add #include <fstream> at the top of your program.
The array should be able to hold 35 elements. Use a symbolic constant to represent the maximum size of an array.
The array has the capability to hold 35 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 buildAr.
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).
Output
Here is the output for this assignment using the binary_hockey.txt file from above.
Chicago Blackhawks UNSORTED Report
Player Goals Assists Points
----------------------------------------------------
Jonathon_Toews 30 41 71
Patrick_Sharp 34 34 68
Patrick_Kane 24 38 62
Marian_Hossa 21 26 47
Tomas_Kopecky 13 27 40
Brent_Seabrook 4 35 39
Duncan_Keith 5 32 37
Dave_Bolland 15 22 37
Bryan_Bickell 16 20 36
Troy_Brouwer 17 17 34
Michael_Frolik 10 24 34
Brian_Campbell 4 21 25
Viktor_Stalberg 11 10 21
Jake_Dowell 6 14 20
Chris_Campoli 3 14 17
Fernando_Pisani 7 9 16
Niklas_Hjalmarsson 3 6 9
Jassen_Cullimore 0 8 8
Ryan_Johnson 1 5 6
Nick_Leddy 3 0 3
Jeremy_Morin 2 1 3
Jordan_Hendry 1 0 1
John_Scott 0 1 1
Jeff_Taffe 0 0 0
Evan_Brophey 0 0 0
Rob_Klinkhammer 0 0 0
Ben_Smith 0 0 0
Brandon_Pirri 0 0 0
Corey_Crawford 0 1 1
Marty_Turco 0 0 0
Chicago Blackhawks SORTED Report
Player Goals Assists Points
----------------------------------------------------
Ben_Smith 0 0 0
Brandon_Pirri 0 0 0
Brent_Seabrook 4 35 39
Brian_Campbell 4 21 25
Bryan_Bickell 16 20 36
Chris_Campoli 3 14 17
Corey_Crawford 0 1 1
Dave_Bolland 15 22 37
Duncan_Keith 5 32 37
Evan_Brophey 0 0 0
Fernando_Pisani 7 9 16
Jake_Dowell 6 14 20
Jassen_Cullimore 0 8 8
Jeff_Taffe 0 0 0
Jeremy_Morin 2 1 3
John_Scott 0 1 1
Jonathon_Toews 30 41 71
Jordan_Hendry 1 0 1
Marian_Hossa 21 26 47
Marty_Turco 0 0 0
Michael_Frolik 10 24 34
Nick_Leddy 3 0 3
Niklas_Hjalmarsson 3 6 9
Patrick_Kane 24 38 62
Patrick_Sharp 34 34 68
Rob_Klinkhammer 0 0 0
Ryan_Johnson 1 5 6
Tomas_Kopecky 13 27 40
Troy_Brouwer 17 17 34
Viktor_Stalberg 11 10 21
Blackhawks Statistics
---------------------
Average Number of Goals Scored 7.67
Average Number of Assists 13.53
Here is what I have for the previous program that I wrote:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
// Setting the array size to 35
const int array_size=35;
// Setting up all prototypes
int buildArrays( string[], int[], int[] );
void printArrays( string[], int[], int[], int );
void sortArrays( string[], int[], int[], int );
int main()
{
// Declaring all variables
string playerNames[array_size];
int goals[array_size];
int assists[array_size],numPlayers;
numPlayers = buildArrays(playerNames, goals,assists); // sets the variable equal to the function
// Displays the unsorted report
cout << "Chicago Blackhawks UNSORTED Report" << endl;
cout << endl;
printArrays( playerNames, goals, assists, numPlayers ); // calls the printArrays function
sortArrays( playerNames, goals, assists, numPlayers ); // calls the sortArrays function
// Displays the sorted report
cout << "\nChicago Blackhawks SORTED Report";
cout << endl;
printArrays( playerNames, goals, assists, numPlayers); // calls the printArrays function
system ("pause");
return 0;
}
// Setting up all functions
/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill the three arrays. It
takes as its arguments the array of strings and two arrays of integers.
It returns the number of valid players that were placed in the arrays.
*****************************************************************************/
int buildArrays( string playerNames[], int goals[], int assists[] )
{
ifstream infile;
infile.open("hockey.txt"); // pulls the source file in
// If statement to execute if the file does not open
if( infile.fail() )
{
// What to display if the file does not open
cout << "The hockey.txt input file did not open";
exit(-1);
}
// Setting i equal to 0
int i=0;
// Loop to execute while inside the file
while( infile )
{
// Pulls in the information from the file
infile >> playerNames[i];
infile >> goals[i];
infile >> assists[i];
// Increments i by 1
i++;
}
infile.close();
return i-1;
}
/*****************************************************************************
void printArrays( )
This function will display the information for the players. For each player,
display the player name, number of goals scored, number of assists, and the
number of points. This function takes as its arguments the three arrays and
the number of players in the arrays.
*****************************************************************************/
void printArrays( string playerNames[], int goals[], int assists[], int numPlayers )
{
// Displays this at the top of the table
cout << "Player\t\t\tGoals\tAssists\tPoints" << endl;
cout << "-----------------------------------------------" << endl;
// Loop to execute to print the information
for( int i = 0; i < numPlayers; i++ )
{
// Pulls in the information from the file and puts it into the table
cout << playerNames[i] << "\t\t" << goals[i] << "\t" << assists[i] << "\t" << goals[i]+assists[i] << endl;
}
cout << "-----------------------------------------------" << endl;
}
/*****************************************************************************
void sortArrays( )
This function will sort the arrays in DESCENDING order based on the number
of goals. Use the selection sort algorithm presented in lecture. This function
takes as its arguments the three arrays and the number of players in the arrays.
*****************************************************************************/
void sortArrays( string playerNames[], int goals[], int assists[], int numPlayers )
{
// Declaring variables
string tmpn;
int tmpg,tmpa;
// Loop to execute to sort the information
for( int i=0; i<numPlayers-1; i++ )
{
for( int j=i+1; j<numPlayers; j++ )
{
if( goals[i] < goals[j] )
{
// Sets two variables equal to one another
tmpg = goals[i];
tmpn = playerNames[i];
tmpa = assists[i];
goals[i]=goals[j];
playerNames[i]=playerNames[j];
assists[i]=assists[j];
goals[j]=tmpg;
playerNames[j]=tmpn;
assists[j]=tmpa;
}
}
}
}
And here is what I have so far for the new program:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
// Setting up all prototypes
int buildAr( playerInfo players[] )
void printAr( playerInfo players[], int )
void sortAr( playerInfo players[], int )
void calcStats( playerInfo players[], int, double &, double & )
int main()
{
system ("pause");
return 0;
}
// Setting up all functions
/*****************************************************************************
int buildAr( )
This function will read the file of data and fill the array. It takes as its
argument the array of playerInfo structures. It returns the number of valid
players that were placed in the arrays.
*****************************************************************************/
int buildAr( playerInfo players[] )
{
}
/*****************************************************************************
void printAr( )
This function will display the information for the players. For each player,
display the player name, number of goals scored, number of assists, and the
number of points. This function takes as its arguments the array of playerInfo
structures and the number of players in the array.
*****************************************************************************/
void printAr( playerInfo players[], int numPlayers )
{
}
/*****************************************************************************
void sortAr( )
This function will sort the arrays in ASCENDING order based on the player name.
Use the selection sort algorithm presented in lecture. This function takes as
its arguments the array of playerInfo structures and the number of players in
the array.
*****************************************************************************/
void sortAr( playerInfo players[], int numPlayers )
{
}
/*****************************************************************************
void calcStats( )
This function will calculate the average number of the goals that were scored
and assists. This function takes as its arguments the array of playerInfo
structures, the number of players in the array, a reference to a double that
will be used to pass-back the average number of goals, a reference to a double
that will be used to pass back the average number of assists.
*****************************************************************************/
void calcStats( playerInfo players[], int numPlayers, double &averageGoals, double &averageAssists )
{
}
I am not quite sure how to convert it. Please help!!