Most of us are familiar with division, using it to perform a variety of ordinary tasks, such as comparing the relative cost of different product sizes (cost per unit) or tracking our car's fuel efficiency or mileage (in the non-metric U.S., miles per gallon). We're also accustomed to rounding the result to a convenient number of digits. But truncation is less familiar, sometimes leaving new programmers wondering if it's an error or at least an oversight. While truncation and modular arithmetic are uncommon in our daily activities, they often help computer scientists solve problems they face.
Operating systems divide secondary storage space (e.g., disk drives) into numbered blocks. Computer scientists have devised various schemes to track the free and in-use (i.e., allocated to a file) blocks. One scheme uses a single bit: 1 indicates the corresponding block is free, and 0 indicates it is allocated. The "free space management" example at the end of the chapter organizes the bits as an array of integers. It demonstrates how functions locate and manage the bits with integer division, modular arithmetic, and bitwise operations. While the problem is an authentic example of integer division and modular arithmetic, it requires advanced concepts and operations.
While money.cpp is a less authentic problem, it's also easier to understand. It converts the value of a given number of U.S. pennies into an equivalent value expressed as U.S. dollars, quarters, dimes, nickels, and pennies.
Test Cases
Based on U.S. currency, test cases for this program are quite trivial.