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!!

Dont get intimidated by the size of the problem statement ...

Lets start with the first part of the problem statement ...

What is the very first problem that you are facing and how do you think it should be solved ?

I kind of understand the functions, I think I can just reuse the same ones as the previous program. But I just don't understand how to put all three into one.

"rather than using multiple arrays to hold the player information, it uses a single array of structures."
So if I write three different functions for 3 different arrays, one to build, one to sort, and one to print, I don't understand how to put them into one

Do you understand the concept of "array of structures " ?\

I think when you prof said "combine all 3 into one" he meant you to use 1 array of structs as opposed to to 3 separate arrays. This is my interpretation, please clarify

Edited 5 Years Ago by abhimanipal: n/a

A structure keeps closely associated data together, kind of like a lump of similar stuff. Your professor wants you to start thinking about how data is associated together, and can be processed together. It simplifies your code, reduces bugs, and enables you to develop much more complex systems. The concept is that of "abstractions". In C++, these are classes (structures of data and associated behavior). For example, the class "Person" may have the following data elements: gender, age, height, weight, marital status, education, income... So, and array of "People" can model groups, such as a club, race, residents of a town, members of a band, or whatever. Clear as mud, right? :-)

So this is what I have got so far for the code:

I am not sure where to put the following two lines in the buildAr function.
playerInfo player;

inFile.read( (char *) &player, sizeof(playerInfo) );
Do I put it in place of lines 81-84?


And for some reason, I am getting a compile error on line 43 of the code

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;


struct playerInfo
{
char playerName[25];
int goalsScored;
int assist;
};

const int array_size=35;

// Setting up all prototypes
int buildArrays( playerInfo[]);
void printArrays( playerInfo[],int );
void sortArrays( playerInfo[],int);
void calcStats(playerInfo[], int, double, double);


int main()
{
double averageGoals, averageAssists;
playerInfo player[array_size];
int n = buildArrays(player);

cout << "Chicago Blackhawls UNSORTED Report" << endl;
cout << endl;

printArrays( player, n );
sortArrays( player, n );

cout << "\nChicago Blackhawls SORTED Report";
cout << endl;

printArrays( player, n );

calcStats( playerInfo, n, &averageGoals, &averageAssists );

cout << endl << "Blackhawks Statistics" << endl;
cout << "---------------------" << endl;
cout << endl << "Average Number of Goals Scored " << averageGoals << endl;
cout << "Average Number of Assists " << averageAssists << endl;


system ("pause");
return 0;
}

// Setting up all functions

/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill in the structure array. It
takes as its arguments the array of playerInfo. It returns the number of valid 
players that were placed in the arrays.
*****************************************************************************/

int buildArrays(playerInfo player[])
{
ifstream infile;

infile.open( "binary_hockey.txt", ios::binary );

if( infile.fail() )
  {
  cout<<"The binary_hockey.txt input file did not open";
  exit(-1);
  system("pause");
  }

int i=0;

while(infile)
  {
  infile>>player[i].playerName;
  infile>>player[i].goalsScored;
  infile>>player[i].assist;
  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 structure playerInfo 
arrays and the number of players in the in player array.
*****************************************************************************/

void printArrays( playerInfo player[], int numPlayers )
{
cout << "Player\t\t\tGoals\tAssists\tPoints" << endl;

cout << "----------------------------------------" << endl;

for(int i=0; i<numPlayers; i++)
  {
  cout << player[i].playerName << "\t\t" << player[i].goalsScored << "\t" << player[i].assist << "\t" << player[i].goalsScored+player[i].assist << 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 playerInfo array and the number of players in the arrays.
*****************************************************************************/

void sortArrays( playerInfo player[], int numPlayers )
{
playerInfo temp;

for(int i=0;i<numPlayers-1;i++)
  {
  for(int j=i+1;j<numPlayers;j++)
    {
    char ch1=player[i].playerName[0];
    char ch2=player[j].playerName[0];

    if(ch1>ch2)
      {
      temp=player[i];
      player[i]=player[j];
      player[j]=temp;
      }
    }
  }

}

/*****************************************************************************
void calcStats( )
This function will calculate average number of goals and average number of 
assists. This function takes as its arguments playerInfo array and the number 
of players in the arrays,reference of average goals and reference of average 
assists.
*****************************************************************************/

void calcStats( playerInfo player[], int numPlayers, double &averageGoals,double &averageAssists )
{

int totalGoals=0, totalAssists=0;

for(int i=0; i<numPlayers; i++)
  {
  totalGoals += player[i].goalsScored;
  totalAssists += player[i].assist;
  }

averageGoals = totalGoals/numPlayers;
averageAssists = totalAssists/numPlayers;

}

Part of your problem is this: void calcStats(playerInfo[], int, double, double); This is the declaration of the calcStats() function. However, where it is defined/implemented, the signature is this: void calcStats(playerInfo[], int, double&, double&); Since the two double& arguments are properly passed as references, so you can update them in the body of the function such that the caller gets the updated information, then you need to correct the declaration. These issues will result in either compilation or runtime errors and/or bugs. I'm sure there are more of the same sort of C/C++ newbie coding errors that I haven't taken the time to suss out, but I think you get my drift. Keep at it, and we'll look again later after you have cleaned up some of these things.

Edited 5 Years Ago by rubberman: n/a

I am working the exact same problem as this right now. so far the only thing I am seeing is around line 58 I put calcStats(player, n, averageGoals, averageAssists); (i replaced playerInfo with player, then I get it to work but I get a lot of crazy numbers. I assume it has to do with establishing something as 0, but i don't know what? please post a working version of this program if you get it going.

This article has been dead for over six months. Start a new discussion instead.