← Back to Book Detail

14 Accumulator Arrays (14/11) -- Patterns for Beginning Programmers

Browse
127%

14 Accumulator Arrays

14 Accumulator Arrays Programs often need to keep track of (in one way or another) multiple things over multiple iterations. In some cases, this can be accomplished with multiple accumulators of the kind discussed in Chapter 13. However, in other cases, it is better to use an array of accumulators. Motivation Many K-12 schools assign numeric grades (on a scale of 0 to 100) to individual students during the year and then, at the end of the year, create a summary report that shows the number of students that were in each centile (i.e., the 90s, the 80s, etc.). If you were asked to write a program for this purpose, you’d know (from Chapter 13) that you should use an accumulator to keep a running count of the number of students in each centile. Since there are eleven different centiles (treating 100 as an entire centile), this means that you need eleven different accumulators. Review If you approached the problem in this way, you might proceed by declaring and initializing eleven different accumulators as follows: int ones, tens, twentys, thirtys, fortys, fiftys, sixtys, seventys, eightys, ninetys, hundreds; ones = tens = twentys = thirtys = fortys = fiftys = sixtys = seventys = eightys = ninetys = hundreds = 0; You then might write the following loop to update these accumulators: int n = data.length; for (int i = 0; i < n; i++) { if (data[i] < 10) ones++; else if (data[i] < 20) tens++; else if (data[i] < 30) twentys++; else if (data[i] < 40) thirtys++; else if (data[i] < 50) fortys++; else if (data[i] < 60) fiftys++; else if (data[i] < 70) sixtys++; else if (data[i] < 80) seventys++; else if (data[i] < 90) eightys++; else if (data[i] < 100) ninetys++; else hundreds++; } Unfortunately, there are three big shortcomings of this approach. First, all of the variables must be declared and initialized individually, and, while its only mildly awkward in this case, if you needed more accumulators it would become very awkward. Second, the nested if statement that is used to update the appropriate accumulator is both awkward, tedious, and error-prone. Finally, since methods in Java can only return a single entity, you could not write a re-usable method to return all of the calculated values, you would have to copy it to wherever it was needed. As you should know, in situations like this (i.e., when you have multiple “related” values) it is better to use an array than to use individual variables. What you may not yet know is how to use an array to solve the centile histogram problem. Thinking About The Problem The first thing to realize is that the variables ones , tens , etc. can be replaced with an array named count . Specifically, count[0] will replace ones , count[1] will replace tens , etc.[1] This facilitates both the declaration and the initialization. The second thing to realize is that the truncation pattern from Chapter 5 can be used to calculate the index associated with a particular centile. Specifically, one can truncate to the 10s place to get the
← Previous Chapter Next Chapter →