Assessment Tool

Lecture 4:  Arithmetic Expressions

Content Tested:  Solving problems using ints and doubles

Lecture Content:

Goals:

Assessment Technique:  Documented Problem Solutions

Purpose:

Instructors can find out if students understand the importance of casting when performing integer division.

Activity:

Today we saw how C expressions are evaluated.  This activity will give you experience using variables and writing expressions.

"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 (You may use the variable at_bats).
 
 
 
 

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.
 
 
 
 

Possible Solution

1.

at_bats is an int
at_bats = appearances - sacrifices - walks - hit_by_pitch;
 

2.

batting_average is a double
batting_average = (double) (singles + doubles + triples + homeruns) /
                  (double) at_bats;
 

3.

on_base_percentage is a double
on_base_percentage = (double) (singles + doubles + triples + homeruns +
                     walks + hit_by_pitch) / (double) appearances;

4.

slugging_percentage is a double
slugging_percentage = (double) (singles + doubles * 2 + triples * 3 +
                       homeruns * 4) / (double) at_bats;
 

Possible Uses of Activity: