"Take me out to the ball game. Take me out to the crowd..." Yes, baseball is on the mind today. You have probably heard of batting averages, on base percentages, and slugging percentages in reference to your favorite baseball player. You'll be working with these ideas in today's exercise. As you work on the exercises below, please state how you arrived at your solution.
Suppose the following variables (all of type int) are declared and initialized
in a C program.
singles
doubles
triples
homeruns
appearances
sacrifices
walks
hit_by_pitch
1. Your first task is to create a variable called at_bats
that stores the appropriate information. In baseball, the number
of at bats a player has is equal to the number of appearances at the plate
minus the number of sacrifices minus the number of walks minus the number
of times the batter is hit by a pitch. Write the appropriate assignment
statement below. (Make sure you include the type of at_bats.)
2. Now, you're interested in calculating the batting average for
a player. The batting average is defined as the number of hits (singles,
doubles, triples, and home runs all count as hits) divided by the number
of at bats. Please state the type of the variable batting_average
and give the appropriate assignment statement given the variables
above.
3. Some say a better statistic is the on-base percentage.
This statistic indicates how often the batter gets on base (either by hitting
the ball, getting hit by a pitch, or walking). The definition of
on-base percentage is the number of hits, walks, and hits by pitches combined
divided by the number of appearances at the plate. Give the appropriate
type for on_base_percentage and assignment statement.
4. Your favorite baseball player is someone they call a "slugger".
There is another baseball statistic called the slugging percentage that
indicates the players ability to get "extra-base base hits" such as doubles,
triples, and home runs. The definition for this is the number of
total bases achieved for all hits divided by the number of at bats.
In baseball, a single = 1 base, a double = 2 bases, a triple = 3 bases,
and a home run = 4 bases. Give the appropriate type for slugging_percentage
and assignment statement.