Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ please, lots of words, but very straightforward à Programming Assignme x

ID: 3747815 • Letter: I

Question

IN C++ please, lots of words, but very straightforward

à Programming Assignme x y D Programming Assignme × Ye Chegg Study| Guided 5, x C ile:/flCUsers/mr nt%202.pdf Programming Assignment 2.pdf 1 13 Problem Statenent You are to develop a program to read Baseball Player objects from an input file. You may reuse your player object from program 1. See that assignment for the requirements for a single player. We are adding additional computations to this program. Each line of the data file will contain one player's name and stats. The format is similar as before, but we are adding in three more fields: firstname lastname plateappearances atbats singlas doubles triples homeruns walks hitbypitch You may assume there are no errors in the data. Iwill not leave data out or put non-numeric data in numeric fields, You must read until the end of the file is found. New Requirements You are to implement a PlayerList data structure/data type that stores players using an internal array in the class. Add players to the List as they are read in from the data file. You may assume that there are no more than 75 players in a single data file. You must implement the following operations on your List along with any other utility functions you might need. You may also need to add operations to your player to encapsulate i/o operations to the Operations for PlayerList Default Constructor .Add a player to the List add items in the order they were found in the file. No sorting is necessary in this version of the program. .Iterate through the List so that you can get each item out for printing o You must implement hasNext) and getNext) on your list for this to work .Clear out the list to make it empty Test if a list isFull Get the size of the list .Compute the team's batting average as an average of the player's individual averages New Operations for the player objects Add methods needed to get the additional compute values: EX: getOnBasePercentage0 etc We are going to compute slugging percentage as well as the batting average. The slugging percentage is a weighted average. It is computed by: . singles 2 doubles +3 triples +4 number of at bats homeruns slugging O Type here to search 11:42 PM 9/14/2018

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
// For maximum player
#define MAX 75

// Class PlayerList definition
class PlayerList
{
// Data member to store data
string firstName;
string lastName;
int plateAppreances;
int atBats;
int singles;
int doubles;
int triples;
int homeRuns;
int walks;
int hitByPitch;
public:

// Default constructor definition to initialize data members
PlayerList()
{
firstName = lastName = "";
plateAppreances = atBats = singles = doubles = triples = homeRuns = walks = hitByPitch = 0;
}// End of default constructor

// Function to read players information and stores it in player list array of objects
int readFile(PlayerList players[])
{
// Counter for records
int counter = 0;

// ifstream object declared
ifstream fRead;
// To store file name
string fileName;

// Accepts file name from the user
cout<<" Enter the name of your input file: ";
cin>>fileName;

// Opens the file for reading
fRead.open(fileName.c_str());

// Checks if the file unable to open for reading display's error message with file name
if(!fRead)
{
cout<<" ERROR: Unable to open the file "<<fileName<<" for reading.";
exit(0);
}// End of if condition

// Loops till end of the file
while(!fRead.eof())
{
// Extracts first name from the file
fRead>>players[counter].firstName;
// Extracts last name from the file
fRead>>players[counter].lastName;

// Extracts plate appreances from the file
fRead>>players[counter].plateAppreances;
// Extracts Bats from the file
fRead>>players[counter].atBats;
// Extracts singles from the file
fRead>>players[counter].singles;
// Extracts doubles from the file
fRead>>players[counter].doubles;
// Extracts triples from the file
fRead>>players[counter].triples;
// Extracts homeRuns from the file
fRead>>players[counter].homeRuns;
// Extracts walks from the file
fRead>>players[counter].walks;
// Extracts hitByPitch from the file
fRead>>players[counter].hitByPitch;
// Increase the student counter by one
counter++;
}// End of while loop
// Closes the file
fRead.close();
// Returns the record counter
return counter;
}// End of function

// Function to display player information
void display(PlayerList players[], int len)
{
// Loops till number of records
for(int x = 0; x < len; x++)
{
// Displays each player information
cout<<endl<<players[x].lastName<<" "<<players[x].firstName<<" "<<players[x].plateAppreances;
cout<<" "<<players[x].atBats<<" "<<players[x].singles<<" "<<players[x].doubles;
cout<<" "<<players[x].triples<<" "<<players[x].homeRuns<<" "<<players[x].walks<<" "<<players[x].hitByPitch;
}// End of for loop
}// End of function

// Function to write the player average to file
void write(PlayerList players[], int len)
{
// ofstream object declared
ofstream fWrite;
// To store total
double totalIn, totalSlu, totalBa;
// To store averages
double individualAvg[len], slugging[len], onBase[len];
// To store file name
string fileName;

// Accepts the file name for writing
cout<<" Enter the name of your output file: ";
cin>>fileName;

// Opens the file for writing
fWrite.open(fileName.c_str());

// Checks if the file unable to open for writing display's error message with file name
if(!fWrite)
{
cout<<" ERROR: Unable to open the file "<<fileName<<" for writing.";
exit(0);
}// End of if condition

// Writes the heading
fWrite<<"BASEBALL REAM REPOERT *** "<<len<<" PLAYERS FOUND IN FILE"<<endl;
fWrite<<"PLAYER NAME : AVERAGE SLUGGING ONBASE%"<<endl;

// Loops till number of records
for(int x = 0; x < len; x++)
{
// Initializes totals to zero
totalIn = totalSlu = totalBa = 0;
// Calculates individual total
totalIn += players[x].singles + players[x].doubles + players[x].triples + players[x].homeRuns;
// Calculates slugging total
totalSlu += players[x].singles + (2 * players[x].doubles) + (3 * players[x].triples) + (4 * players[x].homeRuns);
// Calculates on base total
totalBa += players[x].singles + players[x].doubles + players[x].triples + players[x].homeRuns + players[x].walks + players[x].hitByPitch;
// Calculates individual average
individualAvg[x] = totalIn / players[x].atBats;
// Calculates slugging average
slugging[x] = totalSlu / players[x].atBats;
// Calculates on base average
onBase[x] = totalBa / players[x].plateAppreances;
}// End of for loop

// Loops till number of records
for(int x = 0; x < len; x++)
// Writes data to file
fWrite<<players[x].lastName<<", "<<players[x].firstName<<" "<<fixed<<setprecision(3)<<individualAvg[x]<<" "<<slugging[x]<<" "<<onBase[x]<<endl;
}// End of function
};// End of class

// main function definition
int main()
{
// Declares an array of objects for player list class of size MAX
PlayerList players[MAX];
// Calls the function to read data
int len = players[0].readFile(players);
// Calls the function to display data
players[0].display(players, len);
// Calls the function to write data
players[0].write(players, len);
}// End of main function

Sample Output:

Enter the name of your input file: playerInput.txt

Aaron Hank 13941 12364 2294 624 98 755 1402 32
Jones Chipper 10614 8984 1671 549 38 468 1512 18
Cobb Ty 13099 11434 3053 724 295 117 1249 94
Bench Jonny 8674 7659 1254 381 24 389 891 19
Gwynn Tony 10232 9288 2378 543 85 135 434 24
Snoltz John 1167 948 118 26 2 5 79 3
Enter the name of your output file: Report.txt

File playerInput.txt file contents

Hank Aaron 13941 12364 2294 624 98 755 1402 32
Chipper Jones 10614 8984 1671 549 38 468 1512 18
Ty Cobb 13099 11434 3053 724 295 117 1249 94
Jonny Bench 8674 7659 1254 381 24 389 891 19
Tony Gwynn 10232 9288 2378 543 85 135 434 24
John Snoltz 1167 948 118 26 2 5 79 3

File Report.txt file contents

BASEBALL REAM REPOERT *** 6 PLAYERS FOUND IN FILE

PLAYER NAME : AVERAGE SLUGGING ONBASE%

Aaron, Hank 0.305 0.555 0.373

Jones, Chipper 0.303 0.529 0.401

Cobb, Ty 0.366 0.512 0.422

Bench, Jonny 0.267 0.476 0.341

Gwynn, Tony 0.338 0.459 0.352

Snoltz, John 0.159 0.207 0.200