The local baseball team is computerizing its records. Write a program that computes batting averages and other statistics.
There are 20 players on the team, identified by the player ID 1 through 20.
You will have two input files: roster.txt and statistics.txt
1) The Roster File (roster.txt) -
This file contains 21 lines, first line is an integer indicating the season. The rest is in the format “player ID last name first name” (separated by a space) for each player, as follows:
2012
4 Smith Sam
15 Jones Bob
5 Miller Miles
3 Carter John
11 Apbee Cory
9 Jackson James
2 Zutter Tommy
18 Buzz Bee
8 Duncan Michael
20 Tso Terry
12 Lenn Lance
1 Johnson Albert
6 Knight Owl
19 Neon Bret
13 Ora Adam
16 Elfin Victor
7 Hoo Frank
17 Greene Martin
10 Crane David
14 Rutter Jack
(2) The Statistic File (statistics.txt) -
This file contains the game-by game statistics for the players as a “player ID number, hits, walks, outs”
15 3 2 1
10 2 1 1
9 5 2 1
3 2 2 1
19 1 2 3
20 1 1 1
5 3 1 3
1 2 2 3
11 0 2 2
12 6 3 2
8 3 0 0
7 1 2 3
19 2 2 1
6 5 2 0
5 1 1 1
15 2 5 3
11 1 1 3
14 4 2 3
9 0 1 2
5 1 1 1
15 3 2 1
9 5 2 1
3 2 2 1
20 1 1 1
5 3 1 3
19 1 2 3
12 6 3 2
8 3 0 0
7 1 2 3
19 2 2 1
Here is an example:
3 2 1 1
The example above indicates that during a particular game in the season, player number 3 made 2 hits, 1 walk, and 1 out. Notice, that means they were at bat 4 times.
There might be several lines in the file for the same player. (A player can play more than one game in a season.) The file is organized by the batting line-ups (it is not sorted by player number).
Each player’s batting average is computed by adding the player’s total number of hits, then dividing by the total number of times at bat.
A walk does not count as either a hit or a time at bat when the batting average is being calculated.
When your statistics file contains no data, print message to output file saying, “The 2012 baseball season was cancelled” and end the execution.
Define a struct type PlayerInfo:
1 2 3 4 5 6 7 8
|
struct PlayerInfo
{
int PlayerID;
string LastName;
string FirstName;
int Hits, Walks, Outs;
double Batting, OnBase;
};
|
Create an array of PlayerInfo structs to hold the players’ data. The array is local to main function and passes to other functions as argument.
[code]PlayerInfo Player[20];
Requirements: (your program must follow this logic) Functions must be called in this order inside main function.
1. In the main function, you will call a function GetPlayer to read from Roster file and store the player names and id into the array and return the year for the season.
int GetPlayer ( PlayerInfo [ ] );
(player in first line will store in index of 0, second line in index of 1, third line in index of 2, and etc.)
2. Call a SortPlayerID function to sort them by Player ID. (e.g. use bubble sort or selection sort)
3. Call a PrintRoster function to send the roster for the team with player ID in order to an output file
4. Then call a GetStatistics function to read the statistics file, updating each player’s statistics of hits, walks and outs.
Hints – you need to keep the player identification numbers with the player names and the data in the struct. When you read a player’s game line, look up the player in the array of structs before you adding up the hits, walks and outs (Player ID can identify the index of the array)
5. A function to calculate each player’s batting average and on base average and store them.
(remember to do casting in order to retain the factional part)
batting average = (hits) / (hits + outs)
on base average = (hits + walks) / (total at bats)
6. Call a SortPlayerName function to sort the players by last name
7. A function to Send the players’ statistics to the output file including their Batting Average, On Base Average, in DL list, the best hitter, the worst hitter, the best base runner and the worst base runner. (Note: A player is in the Disable List if he didn’t play at all in the entire season.)
Sample of an output file:
Baseball Season 2012
--------------------------------
Johnson Albert Player 1
Zutter Tommy Player 2
Miller Miles Player 3
and etc……
Tso Terry Player 20
The alphabetical list of players with each player’s statistics (partial list)
Player Number Hits Walks Outs Batting Average On Base Average In Disable List
-------------------------------------------------------------------------------------------------------------------------------------------------------
Apbee Cory 11 xx xx xx 0.xx 0.xx
Buzz Bee 18 xx xx xx 0.xx 0.xx
Carter John 3 Yes
Crane David 10 xx xx xx 0.xx 0.xx
and etc……
Give the last name(s) and batting average of the best hitter (i.e. highest batting average).
Give the last name(s) and on-base average of the best base runner
Give the last name(s) and batting average of the worst hitter
Give the last name(s) and on-base average of the worst base runner
If there is a tie for best or worst, Print all the players’ names.
Example: (might not be true for your input file)
The best hitter with batting average of 0.68: Jones
The worst hitter with batting average of 0.04: Miller, Smith