## Instructions

**Objective**

Write a program to implement arrays in C++.

## Requirements and Specifications

**COSC 1436 Array Programming Problem**

**Filename: baseball.cpp**

A local baseball team wants to automate their data entry and statistics.

Input: zero or more lines input from the keyboard (or a file if you want to do it that way) where each line will have four integers on it. (Assume that this will be true.) The first integer will be a player number 1 through 8 (do not ever step out of the array bounds! I don't care how many players you have.) followed by an integer representing the number of hits, an integer representing the number of walks and an integer representing the number of outs in that order. There may be more than one entry for any given player. Not all players are guaranteed to have entries. Data entry terminates when a negative player number is read.

Output: Produce a table showing each player's cumulative scores and their batting average which will be defined as:

(float) total_hits/(total_hits + total_outs)

Remember that the sum will be an integer. At the end, write a c++ assignment where the team average (do not include the players who did not bat in the average) and (optional/extra credit) find the player with the highest average.

Errors: Input will conform to the above; however, there may be invalid player(s) entered. In that event, just skip that player's whole record and go on. Generate an error message showing all of the invalid data.

A player who never comes to bat will have division by zero in the average. Let's arbitrarily call this a -1.0 because we have to call it something.

Store your data internally as 0..7 but number the output as 1..8. Note that the input is also in terms of 1..8

**Source Code**

`#include `
#include
using namespace std;
int main()
{
cout << "Enter 4 integers per line" << endl;
// Create the arrays to store hits, walks, outs, average and players' id
// There will be a maximum of 8 players
int players[8];
int hits[8];
int walks[8];
int outs[8];
float averages[8];
// Define integers and float to store the values for each player when user enters them
int id, hit, walk, out;
float avg;
for(int i = 0; i < 8; i++)
{
cin >> id >> hit >> walk >> out;
players[i] = id;
hits[i] = hit;
walks[i] = walk;
outs[i] = out;
// Calculate average for this player
if(hit + out == 0)
avg = -1.0;
else
avg = (float)hit/(float)(hit + out);
averages[i] = avg; // save avg into the array of averages
}
// Now, display table
cout << endl;
cout << setprecision(3); // set precision to 3 numbers
cout << left << setw(10) << "Player" << right << setw(20) << "Hits" << setw(20) << "Walks" << setw(20) << "Outs" << setw(20) << "Average" << endl;
int n = 0; // In this variable we will count the number of players with an average different from -1
// Variables to store team avg and best player
float team_avg = 0.0f;
float max_average = 0.0f;
int max_average_player = -1;
for(int i = 0; i < 8; i++)
{
cout << left << setw(10) << players[i] << right << setw(20) << hits[i] << setw(20) << walks[i] << setw(20) << outs[i] << setw(20) << averages[i] << endl;
// Let's use this same loop to calculate the team average and the best player
/*
THE METHOD USED TO CALCULATE
THE TEAM AVERAGE IS: CALCULATE THE AVERAGE BETWEEN ALL INDIVIDUAL AVERAGES
*/
if(averages[i] >= 0.0f) // positive average
{
n++;
team_avg += averages[i]; // sum individial averages
if(averages[i] > max_average) // the current average is higher than the best
{
max_average = averages[i];
max_average_player = players[i];
}
}
}
team_avg = team_avg / (float)n; // divide the sum of the averages by the number of players with a positive average
cout << endl;
// Now, display team average and max average
cout << left << setw(10) << "Team average:" << right << setw(20) << team_avg << endl;
cout << left << setw(10) << "Highest player is #" << max_average_player << " with: " << max_average << endl;