Notes on Integer Division and Truncation
The averaging calculation performed in response to the function call in line 12 of Fig. 4.8 produces an integer result. The sample execution indicates that the sum of the grade values is 846, which, when divided by 10, should yield 84.6—a number with a decimal point. However, the result of the calculation
total / 10 (line 59 of Fig. 4.7) is the integer 84, because
total and 10 are both integers. Dividing two integers results in integer division—any fractional part of the calculation is truncated. We’ll see how to obtain a result that includes a decimal point from the averaging calculation in the next section.
Common Programming Error 4.2
Assuming that integer division rounds (rather than truncates) can ...