Assessment Tool

Lecture 4:  Arithmetic Expressions

Content Tested: Int and Double Types

Lecture Content:

Goals:

Assessment Technique:  Problem Recognition Tasks

Purpose:

This activity allows instructors to see if students understand how to develop algorithms to solve problems and how to choose variables of appropriate types.

Activity:

In today's lecture, we discussed variables of type "int" and "double".  In your group (or as an individual) generate the algorithms to solve the following problems as well as describe the variable names and types you would use if implementing the algorithm in the C language.  Note that you need not write the entire C program.  An example solution to the first problem is given.

1.  A program to calculate the area of a circle given the radius as input.
    Input (what the user might type in):  radius
    Output (what would be displayed to the user):  area

Solution:

Algorithm:  area = PI * radius * radius

Variables and Types:  The type of variable area would be double and the type of variable radius would be double.  PI would be a symbolic constant equal to 3.14.

2.  A program to convert inches (given as a whole number) to miles, feet, and inches.
    Input:  inches (as a whole number)
    Output:  miles, feet, and inches

Solution:

Algorithm:  (Hint:  1 mile is equal to 5280 feet and 1 foot is equal to 12 inches)
 
 
 
 
 
 
 

Variables and Types:
 
 
 
 
 
 
 

3.  A program that calculates the amount of US money in dollars given the number of pennies, nickels, dimes, and quarters.
    Input:  number of pennies, nickels, dimes, and quarters
    Output:  US dollar amount

Solution:

Algorithm:  (Hint:  1 penny = .01 dollars, 1 nickel = .05 dollars, 1 dime = .10 dollars, 1 quarter = .25 dollars)
 
 
 
 
 
 
 

Variables and Types:
 
 
 
 
 
 
 
 

Possible Solutions:

2.  A program to convert inches (given as a whole number) to miles, feet, and inches.
    Input:  inches (as a whole number)
    Output:  miles, feet, and inches

Solution:

Algorithm:  (Hint:  1 mile is equal to 5280 feet and 1 foot is equal to 12 inches)

feet = inches / 12
inches = inches % 12
miles = feet / 5280
feet = feet % 5280
 
 

Variables and Types:

feet, inches, and miles would all be of type int in order to use the % operator.
 
 
 
 
 

3.  A program that calculates the amount of US money in dollars given the number of pennies, nickels, dimes, and quarters.
    Input:  number of pennies, nickels, dimes, and quarters
    Output:  US dollar amount

Solution:

Algorithm:  (Hint:  1 penny = 1 cent, 1 nickel = 5 cents, 1 dime = 10 cents, 1 quarter = 25 cents)

total = num_pennies * .01 + num_nickels * .05 + num_dimes * .10 + num_quarters * .25
 
 

Variables and Types:

num_pennies, num_nickels, num_dimes, and num_quarters would be of type int since it doesn't make sense to have a fractional piece of a coin.  total would of type double since dollars can be fractional (i.e. $3.27).  When writing the C program, one would use a cast for the multiplication of num_pennies and .01 since num_pennies is an integer and .01 is a double.

Alternatively, one could use the following algorithm (making sure all multiplications are done on pairs on integers)

total_cents = num_pennies * 1 + num_nickels * 5 + num_dimes * 10 + num_quarters * 25
total_dollars = (double) total_cents / 100.00

Possible Uses of Activity: