Program 2

You are to write a C program to calculate a baseball player's batting and slugging averages. Your program must compute the player's batting average as the number of hits divided by the number of times at bat, where a hit may be a single, double, triple, or home run. The slugging average is a weighted average computed as follows:

SluggingAvg = (singles + 2 x doubles + 3 x triples + 4 x home runs) divide.gif (55 bytes) times at bat

The program will prompt the user for the number of times the player has been at bat and the number of singles, doubles, triples, and home runs he or she has hit. The program will then calculate and display the batting and slugging averages.

You are to use constants for the weights given to singles, doubles, triples, and home runs and you are to use variables for other values. Be sure to use meaningful identifiers and to document your program fully.

INPUT: From the keyboard, five integers representing the number of times at bat and the number of each type of hit. Be sure to print good prompts to the screen for the user.

OUTPUT: To the screen, prompts for each of the input values and a table showing the number of each type of hit and its weighted value (properly labeled and formatted), followed by the batting and slugging averages printed to three digits of precision.

Sample Run

(To help you distinguish between input and output, prompts and messages appear here in normal font, user input appears in bold. Your program does not print the bold-faced type.)

Enter the number of times at bat
553
Enter the number of singles
79
Enter the number of doubles
38
Enter the number of triples
9
Enter the number of home runs
22

Hit Type
----------

Quantity
----------

Value
-------

single
double
triple
home run

79
38
9
22

79
76
27
88

The player's batting average is 0.268
The player's slugging average is 0.488

Deliverables

Be sure to follow the instructions on the General Information and Coding Standards pages.

  1. Your C source code. Name your program prog2.c
  2. A text-only top-down design. Name this file prog2.tdd
  3. A text-only "Boasts and Confessions" form. Name this file prog2.bc