| CSCI 240 | Spring 2026 |
For this assignment, write a program that will process a data set of information for a season of the Chicago Blackhawks.
For the assignment, use six arrays, each of which should hold a maximum of 40 elements:
The six 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 time information is moved in one array the same move must be made to the other arrays in order to maintain data integrity.
The cpp file that is submitted for grading must be named assign7.cpp.
Declare the variables that are needed for the program. At a minimum, the six arrays, an integer variable to hold the number of players, a string to hold the name of an input file, a string to hold the years of the hockey season, and an ifstream variable. If any other variables are needed, declare them as well.
Get the name of the input file from the user.
Open the input file and make sure that it opened correctly. If it did not open correctly, display an error message that includes the name of the file.
Read the first value from the input file. This is a string that represents the years of the hockey season.
Fill the six arrays by calling the build function. The function returns the number of players that it put into an array. This returned value should be saved in the integer variable that holds the number of players.
Display the number of players that played for the Blackhawks during the season.
Display the six arrays by calling the print function. Make sure to pass a title that includes the years of the season, the six arrays, and the number of players (the integer value returned from the build call).
Sort the arrays by calling the sort function. Make sure to pass the six arrays and the number of players.
Finally, display the six arrays a second time by calling the print function a second time.
The input for this program will be read from a file. The file consists of the years of the season followed by a set of player records. Each player record is made up of six values that are contained on a single line of the file: the player's name, the position code, the number of games played, the number of goals scored, the number of assists, and the plus/minus rating for the player.
2023-2024 Joey_Anderson R 55 5 12 5 Andreas_Athanasiou C 28 2 7 -7 Anthony_Beauvillier L 23 2 4 -10
NOTE: It is okay to assume that if there is a player name, there will be a position, games played, goals, assists, and plus/minus rating.
There are multiple versions available for download:
Make sure to read the "Using an Input File in a CPP program" section of the handout for tips on how to get information from an input file.
This function will read a file of data and fill the six arrays.
It takes as its arguments a reference to an input file stream that represents the input file with the data, an array of strings to hold the names of the players, an array of characters to hold the positions played by each player, and four arrays of integers to hold the number of games played, number of goals scored, number of assists, and plus/minus ratings for the players. It returns an integer: the number of players that were placed in the arrays.
The function should start by declaring any variables that are needed. At a minimum, there should be an integer variable to hold incoming integer values, a character variable to hold the incoming character value, a string variable to hold the incoming string value, and an integer to count the number of players read from the input file.
Use the input operator to read the first player name from input file into the string variable.
In a loop that executes as long as there is information, put the string value that was read into the array of strings, read the remaining input values for the player (the position, games played, goals scored, assists, and plus/minus rating) and put them into the corresponding arrays, update the integer variable that holds the number of players to reflect that a player's information was placed in the arrays, and read the next player name from the input file.
Finally, once all the data has been read from the file, close the file and return the number of players that were placed in the arrays.
This function will display the information for the players.
It takes as its arguments the title for the display, the six arrays to be displayed and the number of players in the arrays. It returns nothing.
For each player, display the player name, the position that the player plays, the number games played, number of goals scored, number of assists, number of points, and plus/minus rating.
The number of points is calculated as follows:
Points = number of goals + number of assists
Use the position_name function described below for the displayed position.
Use the following as a basic format for the output:
Chicago Blackhawks 2023-2024 Player Stats - Unsorted Player Position Games Goals Assists Points Plus/Minus ---------------------------------------------------------------------------------------------------- Joey_Anderson Right Wing 55 5 12 17 +5 Andreas_Athanasiou Center 28 2 7 9 -7 Ethan_Del_Mastro Defense 2 0 0 0 0
The value "Chicago Blackhawks 2023-2024 Player Stats - Unsorted" in the example is the value in the 1st argument passed to the function.
Notice that the + and - signs are printing in front of the non-zero plus/minus ratings. - signs print by default. To get + signs to display, set the showpos flag. To turn off the display of + signs, set the noshowpos flag. Also notice that a rating of 0 does not have a sign displayed.
Formatting Help
There are exactly 100 dashes in the dashed line.
To center the title, use the length() method on the title string to determine how many characters are in the report title. The number of charaters plus (100 - number of characters)/2 is the value that should be used to display the report title in a right-justififed field.
The player name and position should both be displayed left-justififed in fields that are 25 and 13 characters, respectively.
The numeric values are all right justified. They are displayed in fields that are 9 (number of games), 12 (goals scored), 11 (assists), 12 (points), and 13 (plus/minus rating) characters.
Display two newline characters before the report title. Display a single newline character after displaying a player's information.
This function will return a position played by a hockey player.
It takes as its argument a single character: the code that determines what position is played by the player. It returns a string: the position played by the player.
Use the following table to determine a player's position:
| Position Code | Position Name |
|---|---|
| L | Left Wing |
| R | Right Wing |
| C | Center |
| D | Defense |
This function will use the selection sort algorithm that was presented in lecture to sort the arrays in DESCENDING order based on the number of points.
It takes as its arguments the six arrays and the number of players in the arrays. It returns nothing.
As with the print function, the number of points is calculated as follows:
points = number of goals + number of assists
It's important to note that the six 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 an array, it must also swap the corresponding elements in the other five arrays.
Add #include <fstream> and #include <cstdlib> at the top of the program.
Each array should be able to hold 40 elements. Use a symbolic constant to represent the maximum size (maximum number of players) of an array.
Each array has the capability to hold 40 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 build.
Copy the input file to your computer and write the program so that it reads the data from the current directory (i.e. don't specify a file path).
As with program 6, each of the functions MUST have a documentation box that describes the function. This is the last reminder about function documentation that will appear in the assignment write-ups.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
Here is the output for this assignment using the hockey.txt file from above.
Name of input file? hockey.txt
The Chicago Blackhawks used 35 players during the 2023-2024 season.
Chicago Blackhawks 2023-2024 Player Stats - Unsorted
Player Position Games Goals Assists Points Plus/Minus
----------------------------------------------------------------------------------------------------
Joey_Anderson Right Wing 55 5 12 17 +5
Andreas_Athanasiou Center 28 2 7 9 -7
Anthony_Beauvillier Left Wing 23 2 4 6 -10
Connor_Bedard Center 68 22 39 61 -44
Colin_Blackwell Center 44 8 4 12 -2
Louis_Crevier Defense 24 0 3 3 -16
Jason_Dickinson Center 82 22 13 35 +4
Ethan_Del_Mastro Defense 2 0 0 0 0
Ryan_Donato Center 78 12 18 30 -14
MacKenzie_Entwistle Right Wing 67 5 6 11 -29
Nick_Foligno Left Wing 74 17 20 37 -29
Cole_Guttman Center 27 4 4 8 -17
Taylor_Hall Left Wing 10 2 2 4 -3
Reese_Johnson Center 42 2 3 5 -20
Tyler_Johnson Center 67 17 14 31 -35
Seth_Jones Defense 67 8 23 31 -15
Wyatt_Kaiser Defense 32 0 7 7 0
Boris_Katchouk Left Wing 38 5 4 9 -2
Kevin_Korchinski Defense 76 5 10 15 -39
Philipp_Kurashev Center 75 18 36 54 -44
Jaycob_Megna Defense 44 0 2 2 -15
Connor_Murphy Defense 46 2 6 8 -19
Frank_Nazar Center 3 1 0 1 -4
Corey_Perry Right Wing 16 4 5 9 -5
Isaak_Phillips Defense 33 0 6 6 -26
Rem_Pitlick Center 9 0 0 0 -7
Taylor_Raddysh Right Wing 73 5 9 14 -19
Lukas_Reichel Left Wing 65 5 11 16 -29
Filip_Roos Defense 4 0 0 0 -4
Zachary_Sanford Left Wing 18 0 4 4 -3
Brett_Seney Left Wing 4 0 0 0 0
Landon_Slaggert Left Wing 16 1 3 4 +1
Jarred_Tinordi Defense 52 0 9 9 -27
Alex_Vlasic Defense 76 2 14 16 -4
Nikita_Zaitsev Defense 38 2 5 7 -5
Chicago Blackhawks 2023-2024 Player Stats - Sorted
Player Position Games Goals Assists Points Plus/Minus
----------------------------------------------------------------------------------------------------
Connor_Bedard Center 68 22 39 61 -44
Philipp_Kurashev Center 75 18 36 54 -44
Nick_Foligno Left Wing 74 17 20 37 -29
Jason_Dickinson Center 82 22 13 35 +4
Tyler_Johnson Center 67 17 14 31 -35
Seth_Jones Defense 67 8 23 31 -15
Ryan_Donato Center 78 12 18 30 -14
Joey_Anderson Right Wing 55 5 12 17 +5
Lukas_Reichel Left Wing 65 5 11 16 -29
Alex_Vlasic Defense 76 2 14 16 -4
Kevin_Korchinski Defense 76 5 10 15 -39
Taylor_Raddysh Right Wing 73 5 9 14 -19
Colin_Blackwell Center 44 8 4 12 -2
MacKenzie_Entwistle Right Wing 67 5 6 11 -29
Boris_Katchouk Left Wing 38 5 4 9 -2
Andreas_Athanasiou Center 28 2 7 9 -7
Corey_Perry Right Wing 16 4 5 9 -5
Jarred_Tinordi Defense 52 0 9 9 -27
Connor_Murphy Defense 46 2 6 8 -19
Cole_Guttman Center 27 4 4 8 -17
Wyatt_Kaiser Defense 32 0 7 7 0
Nikita_Zaitsev Defense 38 2 5 7 -5
Isaak_Phillips Defense 33 0 6 6 -26
Anthony_Beauvillier Left Wing 23 2 4 6 -10
Reese_Johnson Center 42 2 3 5 -20
Zachary_Sanford Left Wing 18 0 4 4 -3
Landon_Slaggert Left Wing 16 1 3 4 +1
Taylor_Hall Left Wing 10 2 2 4 -3
Louis_Crevier Defense 24 0 3 3 -16
Jaycob_Megna Defense 44 0 2 2 -15
Frank_Nazar Center 3 1 0 1 -4
Filip_Roos Defense 4 0 0 0 -4
Ethan_Del_Mastro Defense 2 0 0 0 0
Brett_Seney Left Wing 4 0 0 0 0
Rem_Pitlick Center 9 0 0 0 -7
The data for this program will be read from an input file rather than using standard input (cin) or the random number generator. The file should be downloaded and saved. As mentioned earlier, there are multiple versions available for download from Blackboard, the autograder, and the course website. They are:
A file consists of the years of the season followed by a set of player records. Each player record is made up of six values that are contained on a single line of the file: the player's name, the position code, the number of games played, the number of goals scored, the number of assists, and the plus/minus rating for the player.
2023-2024 Joey_Anderson R 55 5 12 5 Andreas_Athanasiou C 28 2 7 -7 Anthony_Beauvillier L 23 2 4 -10
To start working with an input file, a couple of extra include statements are needed for the fstream and cstdlib libraries.
In previous assignments, when a user entered the values for input, the C++ code used cin and the input operator (>>) to get the information from the keyboard and save it in a variable. cin is an input stream. To get information from a file, an input file stream is needed.
ifstream infile; //input file stream variable this will be used instead of cin
The input file stream variable (infile in the example) will be used in place of cin when information is needed.
Now that the input file stream variable has been created, it needs to be "connected" with the text file that holds the information to be read. The "connection" is created by opening the file:
infile.open( "hockey.txt" ); //open the file for reading
OR
infile.open( filename ); //open the file for reading -- assuming the name
//of the file to open is in a string variable
//named filename
For this assignment, use version 2 with the string variable.
The open statement will try to open the file specified ("hockey.txt" or whatever file name is saved in the filename variable).
Windows Users: the input file MUST be located in the same directory as the CPP file.
Mac Users: there are three options available to handle the input file.
Option 1: put in the set of double quotes ("") between the parenthesis for the open command, find where the file has been saved on the Mac, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on the Mac, between the quotes. If this option is used, before handing in the CPP file, remove the path name from the file name so that it only has the file name between the double quotes.
Option 2 for newer versions of XCode (12+): follow the directions in the PDF file found here http://faculty.cs.niu.edu/~byrnes/csci240/FilesAndXCode13.pdf
Option 3 for older versions of XCode: follow the directions in the PDF file found here http://faculty.cs.niu.edu/~byrnes/csci240/FilesAndXCode.pdf
onlinegdb Users: the input file must be uploaded to the online
compiler. To do so, click the cloud/up arrow button (
)
and select the input file. It should open in a tab next to the CPP file in the
online compiler.
Once the file has been opened, it's important to make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly, use something similar to:
if( infile.fail() ) //if the input file failed to open
{
cout << endl << "input file did not open" << endl;
exit(-1); //stop execution of the program immediately
}
In the previous programs, the values have been read by executing a cout/cin combination. Since this program is reading input from a file, substitute the name of the input file stream (infile in the example) in place of cin. The cout statement is not needed with the data being read from a file.
So, if a program had something like:
cout << "Player Name? "; cin >> value;
It should now be:
infile >> value;
When writing a read loop using data from a file, the loop should test to see if there is data in the file. One way to do this is to use the input file stream variable as a boolean variable:
while( infile )
As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is ONLY successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern that uses a priming and secondary read should be followed.
Finally, once all the data has been read from the file, the file should be closed. To do this, execute the following:
infile.close();