How to create an histogram of asteriks from a Java array of number repetitions?

3.8k views Asked by At

I have an array with repetitions of numbers and I need to show them in an "histogram" made by "*". The histogram should looks like this:

            *
        *   *
  *     *   *
  *     * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
1 2 3 4 5 6 7 8 9 10

I have this array

int[] repetition = new int[] {4,6,4,4,6,5,7,4,3,3};

and I was able to print it horizontally like this:

1*****
2******
3****
4****
5******
6*****
7*******
8****
9***
10***

How can I create the vertical histogram?

5

There are 5 answers

2
skrtbhtngr On BEST ANSWER

First you compute the maximum height of the histogram.

max=0;
for(int i: repetition)
    if(i>max)
        max=i;

Then, you print like this, from top to bottom:

for(j=max;j>=1;j--){                  //For each possible height of the histogram.
    for(k=0;k<repetition.length;k++)  //Check height of each element
        if(repetition[k]>=j)
            System.out.print("*");    //Print * if the k-th element has at least a height j.
        else
            System.out.print(" ");    //Else, do print blank space
    System.out.println();             //Newline after every row.
}

PS: This is just an idea, not a complete working code, and it works only for positive height values!

0
erickson On

It sounds like you have code to "bin" the data, effectively creating the histogram. Add some code to track the maximum count across all the bins. So, in your example above, the maximum would be 8 (from bin 7).

Then, set a threshold, starting at that maximum, and counting down to one. On each iteration, print lines with asterisks in the columns corresponding to the bins that meet or exceed the threshold. So on the first line, only column 7 would get an asterisk, because it's the only bin that meets the current threshold of 8. The next line (threshold 7), columns 5 and 7 would get asterisks. And so on.

Hopefully that's enough help to write the code yourself.

0
Matthias H On

Integer is used to use the Collections max function.

public static void main(String[] args) {
        Integer[] repetition = new Integer[] { 4, 6, 4, 4, 6, 5, 7, 4, 3, 3 };
        int maxValue = Collections.max(Arrays.asList(repetition));
        System.out.println("Maximum: " + maxValue);
        for (int i = maxValue; i > 0; i--) {
            for (int j = 0; j < repetition.length; j++) {
                if (repetition[j] >= i) {
                    System.out.print(" * ");
                } else {
                    System.out.print("   ");
                }
            }
            System.out.println();
        }
        for (int j = 0; j < repetition.length; j++) {
            System.out.print(" " + (j + 1) + " ");
        }
    }
0
arjuns On

I am not going to give you any code, but I can give you an idea to start off of.

For the vertical histogram, even though it looks like it is being printed top to bottom, it's not; you just can't print vertically. Using a loop (the highest value in the array being the starting value, 0 being the sentinel value, and decrementing by 1 every iteration) where every iteration represents a horizontal line on the histogram, you should determine which x-label's asterisk(s) need to be printed on that line, and for those labels that don't have an asterisk on that line, place a space there instead (for formatting).Then, create another loop and use it to list all the x-labels at the bottom.

Hope this helps!

1
hyper-neutrino On
public static final void printHistogram(int[] coll) {
        int max = coll[0];
        for (int i : coll)
            max = i > max ? i : max;
        boolean[][] array = new boolean[max][coll.length];
        for (int i = coll.length - 1; i >= 0; i--) {
            for (int j = 0; j < coll[i]; j++) {
                array[j][i] = true;
            }
        }
        for (int i = array.length - 1; i >= 0; i--) {
            boolean[] booleans = array[i];
            for (boolean b : booleans) {
                System.out.print(b ? '*' : ' ');
            }
            System.out.println();
        }

        for (int i = 1; i <= array.length; i++) {
            System.out.print(i);
        }
    }

This works as expected.