For this assignment, write a program that will process a data set of information
ID: 3532202 • Letter: F
Question
For this assignment, write a program that will process a data set of information for the Chicago Blackhawks. 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, declare four arrays, each of which will hold a maximum of 25 elements:
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 time information is moved in one array the same move must be made to the other arrays in order to maintain data integrity.
NOTE: all of the functions mentioned in this logic are described below.
Fill the four arrays by calling the buildArrays() function.
Display the title "Chicago Blackhawks UNSORTED Report"
Display the four arrays by calling the printArrays() function.
Sort the four arrays by calling the sortArrays() function.
Display the title "Chicago Blackhawks SORTED Report"
Display the sorted arrays by calling the printArrays() function.
The input for this program will be read from a disk file named hockey.txt. The file consists of a set of player records. Each record represents one player and is made up of four values that are all contained on one line of the file: the first is the player name, the second is the number of goals the player scored, the third is the number of assists for the player, and the fourth is the plus/minus rating for the player. The file resembles the following:
NOTE: You may assume that if there is a player name, then there will be a number of goals, assists, and plus/minus rating.
This function will read the file of data and fill the four arrays. It takes as its arguments the array of strings and three arrays of integers. It returns the number of valid players that were placed in the arrays.
Suggested logic:
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)
The above code will open the file and then make sure that the file did indeed open correctly.
Or if you want to read a string:
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.
This function will display the information for the players. For each player, display the player name, number of goals scored, number of assists, number of points, and plus/minus rating.
This function takes as its arguments the four arrays and the number of players in the arrays.
The number of points is calculated as follows:
Use the following as a basic format for the output:
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.
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.
As with the printArrays function, the number of points is calculated as follows:
This function takes as its arguments the four arrays and the number of players 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 an array, it must also swap the corresponding elements in the other three arrays.
Add #include <fstream> at the top of your program.
Each array should be able to hold 25 elements. Use a symbolic constant to represent the maximum size (maximum number of players) of an array.
Each array has the capability to hold 25 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).
Here is the output for this assignment using the hockey.txt file from above.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstring>
#define SIZE 25
using namespace std;
int buildArrays(char playerNames[SIZE][50], int goals[], int assists[], int ratings[]);
void printArrays( char playerNames[SIZE][50], int goals[], int assists[], int ratings[], int numPlayers);
void sortArrays (char playerNames[SIZE][50], int goals[], int assists[], int ratings[], int numPlayers );
int main()
{
char playerNames[SIZE][50];
int goals[SIZE];
int assists[SIZE];
int rating[SIZE];
int numPlayers;
numPlayers=buildArrays(playerNames,goals,assists,rating);
cout << ' ' << "Chicago Blackhawks Unsorted List";
printArrays(playerNames,goals,assists,rating,numPlayers);
sortArrays(playerNames,goals,assists,rating,numPlayers);
cout << ' ' << "Chicago Blackhawks Unsorted List";
printArrays(playerNames,goals,assists,rating,numPlayers);
return 0;
}
int buildArrays(char playerNames[SIZE][50], int goals[], int assists[], int ratings[])
{
FILE* fp=fopen("hockey.txt","r");
char ch;
int i=0;
char p[50];
char g[50];
char a[50];
char r[50];
char buffer[100];
while(1)
{
fscanf(fp,"%s",p);
fscanf(fp,"%s",g);
fscanf(fp,"%s",a);
fscanf(fp,"%s",r);
strcpy(playerNames[i],p);
goals[i]=atoi(g);
assists[i]=atoi(a);
ratings[i]=atoi(r);
i++;
if(feof(fp))
break;
}
return i-1;
}
void printArrays( char playerNames[SIZE][50], int goals[], int assists[], int rating[], int numPlayers)
{
cout << fixed << showpoint <<setprecision(1) << left;
cout << endl;
cout << endl;
cout << setw(30) << "Players" << setw(10) << "Goals" << setw(10) << "Assists" << setw(10)
<< "Points" << setw(10) << "Rating";
cout << endl;
cout << "---------------------------------------------------------------------";
cout << endl;
for(int i=0;i<numPlayers;i++)
{
cout<<setw(30)<<std::noshowpos<<playerNames[i]<<setw(10)<<goals[i]<<setw(10)<<assists[i]<<setw(10)<<goals[i]+assists[i]<<setw(10)<<std::showpos<<rating[i]<<endl;
}
}
void sortArrays (char playerNames[SIZE][50], int goals[], int assists[], int rating[], int numPlayers )
{
int points[SIZE];
for(int i=0;i<numPlayers;i++)
{
points[i]=goals[i]+assists[i];
}
char tempName[30];
int temp;
int swapPos;
for ( int i = 0 ; i<numPlayers-1; i++ ){
swapPos = i;
for ( int j = i + 1 ; j<numPlayers ; j++ ){
if ( points[swapPos] < points[j] )
swapPos = j;
}
if ( swapPos != i ){
strcpy(tempName,playerNames[i]);
strcpy(playerNames[i],playerNames[swapPos]);
strcpy(playerNames[swapPos],tempName);
temp=goals[i];
goals[i]=goals[swapPos];
goals[swapPos]=temp;
temp=assists[i];
assists[i]=assists[swapPos];
assists[swapPos]=temp;
temp=rating[i];
rating[i]=rating[swapPos];
rating[swapPos]=temp;
//points need to be updated as well or else array will be a mess
temp=points[i];
points[i]=points[swapPos];
points[swapPos]=temp;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.