Search code examples
javaarraysloopsmethodshistogram

Loop structure for histogram


I'm trying to write a Java program that generates a histogram of asterisks for each occurrence of a value in an array.

If the elements are, respectively, 0,1,2,3,4,5,6,7,8,9 the output should have an asterisk for each occurrence. For example,

0:*
1:*
2:*
3:*
4:*
5:*
6:*
7:*
8:*
9:*

However, my output is

0:**********
1:
2:
3:
4:
5:
6:
7:
8:
9:

The following code below is my own.

public static void drawHistogram(double[] array) {

    String count = "";

    for (int i = 0; i < array.length; i++) {
        if (array[i] >= 0 && array[i] < 1) {
            count += "*";
        } else if (array[i] >= 1 && array[i] < 2) {
            count += "*";
        } else if (array[i] >= 2 && array[i] < 3) {
            count += "*";
        } else if (array[i] >= 3 && array[i] < 4) {
            count += "*";
        } else if (array[i] >= 4 && array[i] < 5) {
            count += "*";
        } else if (array[i] >= 5 && array[i] < 6) {
            count += "*";
        } else if (array[i] >= 6 && array[i] < 7) {
            count += "*";
        } else if (array[i] >= 2 && array[i] < 8) {
            count += "*";
        } else if (array[i] >= 2 && array[i] < 9) {
            count += "*";
        } else if (array[i] >= 9 && array[i] < 10) {
            count += "*";
        } else if (array[i] >= 10 && array[i] < 11) {
            count += "*";
        }
    }
    for (int j = 0; j <= 10; j++) {
        System.out.print(j + count);
        count = "";
        System.out.println();
    }
}

How can I fix this issue?


Solution

  • This solution uses (int) Math.floor(array[i]) to choose the bracket into which to put the double value, thus getting rid of the multiple if-then-else statements. I've also used StringBuilder instead of String to make the repeated concatenation of asterisks a little more efficient.

    public static void drawHistogram(double[] array) {
    
        StringBuilder histoGram[] = new StringBuilder[11];
        for (int i = 0; i < histoGram.length; i++) {
            histoGram[i] = new StringBuilder();
        }
    
        for (int i = 0; i < array.length; i++) {
            int bracket = (int) Math.floor(array[i]);
            if (bracket >= 0 && bracket < histoGram.length) {
                histoGram[bracket].append("*");
            }
        }
        for (int j = 0; j < 11; j++) {
            System.out.format("%02d: %s\n", j, histoGram[j].toString());
        }
    }
    

    Test main method:

    public static void main(String args[]) {
        double[] testValues = new double[100];
        for (int i = 0; i < 100; i++) {
            testValues[i] = Math.random() * 11.0;
        }
        drawHistogram(testValues);
    }
    

    Sample output:

    00: *******
    01: ********
    02: ***********
    03: ************
    04: ********
    05: **********
    06: *******
    07: ********
    08: **********
    09: ************
    10: *******