(Latest Revision: 05/05/2001)
SIXTH CS 1500 SOLO PROGRAM: Batting Statistics
OBJECTIVE:
The purpose of this programming assignment is to get practice using files,
arrays, and for-loops.
THE ASSIGNMENT:
Write a program that computes batting averages.
INPUT:
You must write the program with certain facts, assumptions, and
preconditions in mind:
You will need to use an editor to make one or more versions of
teamData for your testing.
Your program will open teamData with this statement:
ifstream infile("teamData") ;
As stated above, there will be one player game record per line in
teamData.
The meaning of this player game record:15 3 1 6
is
that, in one particular game played by "our team,"
player number 15 got 3
hits, 1 walk, and 6 outs.
Consider this example of what might be part or all of the contents of
teamData:
3 1 2 3
12 2 0 4
7 1 1 3
12 1 1 2
7 4 1 1
12 3 1 2
This data tells us that player #3 got 1 hit, 2 walks, and 3 outs in a game.
Player #12 got 2 hits, no walks, and 4 outs in one game, and in two other
games player #12 got a total of 4 more hits, 2 more walks, and 4 more outs.
Also player #7 got a total of 5 hits, 2 walks, and 4 outs in two games.
There are 25 players, numbered from 0 to 24. As is the case above for
player #3, teamData may contain just one player game record for a
particular player. As is the case for players #12 and #7 above,
teamData may contain more than one player game record for a
particular player. It is also possible that teamData contains
no records at all for some of the players. There is nothing in the
data above corresponding to most of the players. For example, there are no
records corresponding to player #1.
Here is another small example of what might be part or all of the contents of
teamData:
3 1 2 3
12 2 0 4
7 1 1 3
8 2 2 4
24 4 2 1
9 2 1 6
16 3 0 1
12 1 1 2
1 3 0 9
7 4 1 1
20 6 3 23
12 3 1 2
4 2 1 4
OUTPUT:
The program computes a total number of hits, walks, and outs for each of the
players (player #0 through player #24.) It is important to understand that
sometimes the program has to add numbers from two, three, or more records in
order to get the totals. There is no way to know ahead of time how many
records happen to be in teamData for each player. Also, if
no records occur in teamData for some player, then the
program has to get all zeros for the totals of that player.
Totals are computed as sums taken over all the records corresponding
to the player. For example, for player #7 in the last sample above, the
total number of hits is 5=1+4, the total number of walks is 2=1+1, and the
total number of outs is 4=3+1. For player #12, the totals are 6, 2, and 8.
The program also computes the batting average of each player using the
following
formula: batting_average = 1000 * total_hits / (total_hits + total_outs)
The number of walks is not used in the formula for computing the batting
average.
The program must print a tabular report on all the players, like this sample
output:
Batting Statistics
Player # Batting Av Walks
0 0 0
1 250 0
2 0 0
3 250 2
4 333 1
5 0 0
6 0 0
7 556 2
8 333 2
9 250 1
10 0 0
11 0 0
12 429 2
13 0 0
14 0 0
15 0 0
16 750 0
17 0 0
18 0 0
19 0 0
20 207 3
21 0 0
22 0 0
23 0 0
24 800 2
You can tell from the sample output that
- This sample output corresponds to the last sample input given above.
- The batting average is expressed as an integer between 0 and 1000. It
does not contain a decimal point.
- a player with no hits or outs is assigned a batting average of zero.
The output of your program must follow these same rules.
The tabular report that your program prints out must have a nice neat
heading. The heading must include a centered title of the table, and
centered titles of each of the columns of the table. Do a nice neat job.
You can do it just like the sample output above if you wish.
THE DESIGN OF THE PROGRAM:
There must be at least two functions besides main in the program,
each having one or more parameters. You may find you will get a better
design if you use more than two functions.
You must create your own design of this program. I am expecting you to
employ good principles of top-down design. Substantial credit will be
withheld if you do not create a program that uses functions
appropriately.
Some particulars:
- Make sure that each function does one thing well.
- Put no more than one return statement in any function.
- Use exit() statements sparingly -- no more than one in each
function.
I want you to get the experience of writing a program that uses arrays.
Therefore I am requiring that your program use arrays to store counts
of hits, walks, and outs. Use three arrays of 25 integers. The indices
will run from 0 through 24, which is exactly the set of player numbers.
Name the arrays hits, walks, and outs.
Because I am going to test all the programs by compiling and running them on
different versions of the input file, I need everyone in the class to use
exactly the same name for the input file. Therefore,
please name your file teamData and use the command:
ifstream infile("teamData") ;
in your program.
To see some C++ code with similarities to the code you need to write, look
at the
sample program
that reads and prints golf scores. Also check out
the sample input
and
corresponding output
for the golf scores problem.
Note: Come to class to get more discussion of how to design a solution to
this problem.
FORM AND FORMATTING REQUIREMENTS:
Write appropriate header comments for each of your functions. Header
comments are the comments that appear at the beginning of the function,
telling what it's purpose is and what the preconditions and postconditions
are, and so forth. You may pattern your header comments after the example
code in your text book, or you may use
this information
as your guide.
At the beginning of your program file put a header comment like
this:
/* PROGRAM Batting Statistics */
/*
Name: Gordon Goodguy
User Name: goodge
Course: CS 1500, Computer Programming I
Instructor: John Sarraille
Date: November 31, 2001
*/
Of course, in place of "Gordon Goodguy," you must put your own full name.
In place of "goodge" put your user (login) name. In place of "November 31,
2001" put the date that you finished the program. Note that the comment
delimiters /* and */ are important. They need to be placed correctly or you
will get compiler errors.
TESTING:
Since the specifications allow the file teamData to be empty, you
will need to make at least two versions of teamData for testing.
Here's a nice way of managing different versions of teamData for testing:
WHAT TO TURN IN:
Before midnight on the due date, e-mail the following two items
to me:
- A copy of the source code, properly documented.
- A filtered script showing an appropriate number of
test runs with appropriate inputs.
My e-mail address is:
john@ishi.csustan.edu
Please use the following subject lines exactly for the
e-mails:
For the source file: CS1500_Solo6_Source
and for the script file: CS1500_Solo6_Script.
Note that there are no spaces in these subject lines. If you like, you can
copy and paste the subject lines right from this document.
Thanks! Your use of these subject lines will really be a big
help to me when I try to sort through the perhaps hundred e-mail messages
that will be in my electronic mailbox!
WHEN IS THIS ASSIGNMENT DUE?
Look for the due date in
the class schedule.
(It's at the top level of the class directory.)