Skip to main content
Engineering LibreTexts

16.9: Building a Histogram

  • Page ID
    18918
  • The previous code is repetitious, but it is acceptable as long as the number of ranges is small. But suppose we wanted to keep track of the number of times each score appears. We would have to write 100 lines of code:

    int count0 = inRange(scores, 0, 1);
    int count1 = inRange(scores, 1, 2);
    int count2 = inRange(scores, 2, 3);
    ...
    int count99 = inRange(scores, 99, 100);
    

    What we need is a way to store 100 counters, preferably so we can use an index to access them. In other words, we need another array!

    The following fragment creates an array of 100 counters, one for each possible score. It loops through the scores and uses inRange to count how many times each score appears. Then it stores the results in the array:

    int[] counts = new int[100];
    for (int i = 0; i < counts.length; i++) {
        counts[i] = inRange(scores, i, i + 1);
    }
    

    Notice that we are using the loop variable i three times: as an index into the counts array, and as two arguments for inRange. The code works, but it is not as efficient as it could be. Every time the loop invokes inRange, it traverses the entire array.

    It would be better to make a single pass through the array, and for each score, compute which range it falls in and increment the corresponding counter. This code traverses the array of scores only once to generate the histogram:

    int[] counts = new int[100];
    for (int i = 0; i < scores.length; i++) {
        int index = scores[i];
        counts[index]++;
    }
    

    Each time through the loop, it selects one element from scores and uses it as an index to increment the corresponding element of counts. Because this code only traverses the array of scores once, it is much more efficient.

    • Was this article helpful?