< previous page page_757 next page >

Page 757
PercentNearest 10 Percent
36.957
40
17.391
20
13.043
10
6.522
10
26.087
30
100.000
110

This is a classic example of round-off error. Case Study Follow-Up Exercise 3 asks you to rewrite the functions that create the bar chart using a smaller interval, so that an asterisk represents 5 percentage points instead of 10.
Testing and Debugging
Errors with multidimensional arrays usually fall into two major categories: index expressions that are out of order and index range errors. In addition, undefined-value errors may result from trying to access uninitialized components.
As the number of dimensions increases, so does the likelihood of a subtle logic error. The syntax of your nested loop structure may be valid, but what you intended to have happen may not be what you coded. Using meaningful identifiers for your loop control variables will help. If you were to use i, j, and k as the loop control variables in the department store example, it would be easy to interchange them by mistake. If you use item, store, and month, you are less likely to confuse the indices. Even if you have the index expressions in the correct order following the array name, you can end up with index range errors if you use the wrong upper (or lower) limits for an index variable that is controlling a loop.
Suppose we were to expand the Election program to accommodate 10 candidates and four precincts. Let's declare the votes array as
int votes[4][10];
The first dimension represents the precincts, and the second represents the candidates. An example of the first kind of errorincorrect order of the index expressionswould be to print out the votes array as follows.

 
< previous page page_757 next page >