Our familiarity with time is a significant advantage of the Time structure example, allowing us to focus more on structures and less on the illustrating problem. But, basing the example on time also presents a disadvantage: What does the "60" appearing in make_time(int s) and add mean? There are 60 seconds in a minute and 60 minutes in an hour. But, it isn't clear from a casual examination of the functions which "60" they use. While it doesn't make a difference in the Time example, it does when the unit numbers differ.
The U.S. is the only industrialized nation not using the metric system in its commercial and day-to-day activities. Instead, it uses the "United States customary system." In this system
We can represent a total volume with a structure1 just as we did in the Time example. However, the relationships between the units make understanding the numbers in the resulting program easier.
The American Volume Problem
Write a C++ program in three files
American.h: contains a structure specification and function prototypes
American.cpp: contains the structure functions
driver.cpp: code to test the American structure and functions (main will be in this file)
Create a structure named American with three fields to represent liquid measures using the U.S. customary units:
ounce
tablesp
teasp
The structure does not need to handle fractional values for any of the units (i.e., make the fields integers)
The program must have the following structure functions:
Two functions to make an instance of the American structure
American make_American(int oz, int Tbsp, int tsp);
American make_American(int tsp);
add
has two American objects parameters
sums the objects without changing either one
returns a new, normalized American object to represent the sum
normalization means
0 ≤ teasp < 3; 3 teaspoons should "carry" to make a tablespoon
0 ≤ tablesp < 2; 2 tablespoons should "carry" to make a fluid-ounce
the add function may not use branch or loop statements
print
has a single American object parameter
prints the objects in this format: 3 fl-oz, 1 Tbsp, 2 tsp
The program must have a driver testing all the structure functions.
Unit Analysis
Mathematicians might use raw numbers when solving some problems, but engineers and scientists always attach units to numbers. For example, 5 inches is quite different than 5 light-years. We can perform arithmetic operations on the units in the same way as numbers and variables, which is especially helpful for multiplication and division. Carrying the units through the algebraic operations solving a problem can help us avoid some errors. In the American structure problem, arranging the equalities I, II, and III so that the units cancel2 out helps us identify and understand the numbers used in the following program.
Converting An Integer To An American Structure
(a)
(b)
(c)
temp.ounce = x / 6; // each group of 6 tsp = 1 ounce
x %= 6; // the remaining tsp that didn't fit in an ounce
temp.tablesp = x / 3; // each group of 3 Tbsp = 1 ounce
temp.tsp = x % 3; // the remaining Tbsp that didn't fit in an ounce
Converting tablespoons and teaspoons to fluid onces.
We use Figure 1, equality I, to convert from teaspoons to tablespoons. We begin with x tsp, and our target is Tbsp. So, we arrange equality I so that the tsp units cancel2.
Similarly, we use Figure 1, equality III, to convert from teaspoons to fluid ounces. We begin with x tsp, and our target is fl-oz. So, we arrange III so that the tsp and Tbsp units cancel2.
Converting an integer to an American structure takes several statements (most of the second overloaded make_American function). The added complexity results from the requirement of saving each field as a whole number. temp is an American structure object, and x, the function parameter, is the number of teaspoons that the functions convert to an American structure.
Converting An American Structure To A Single Integer
Converting fluid onces and tablespoons to teaspoons.
Again, we use Figure 1, equality I, to perform the needed conversion operation, but we convert from tablespoons to teaspoons this time. We begin with x Tbsp, and our target is tsp. So, we arrange equality I so that the Tbsp units cancel2.
Following the pattern of the previous figure, we use Figure 1, equality III, to convert fluid ounces to teaspoons. We begin with x fl-oz, and our target is tsp. So, we arrange III so that the fl-oz and Tbsp units cancel2.
A statement, based on the derived conversion operations, converting an American structure object, am1, to an integer, i1.
American Program Listings
Typically, the U.S. system represents measurements with a single value in one unit rather than multiple values with different units. So, for example, a recipe would require 11 tsp of an ingredient, not 1 fl-oz, 1 Tbsp, and 2 tsp.
"Canceling" is a shorthand term for dividing a number, variable, or unit by itself. For example, tsp / tsp = 1. In general, we can say x / x = 1; x ≠ 0.