How to create an histogram of asteriks from a Java array of number repetitions? -
i have array repetitions of numbers , need show them in "histogram" made "*". histogram should looks this:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1 2 3 4 5 6 7 8 9 10
i have array
int[] repetition = new int[] {4,6,4,4,6,5,7,4,3,3};
and able print horizontally this:
1***** 2****** 3**** 4**** 5****** 6***** 7******* 8**** 9*** 10***
how can create vertical histogram?
first compute maximum height of histogram.
max=0; for(int i: repetition) if(i>max) max=i;
then, print this, top bottom:
for(j=max;j>=1;j--){ //for each possible height of histogram. for(k=0;k<repetition.length;k++) //check height of each element if(repetition[k]>=j) system.out.print("*"); //print * if k-th element has @ least height j. else system.out.print(" "); //else, print blank space system.out.println(); //newline after every row. }
ps: idea, not complete working code, , works positive height values!
Comments
Post a Comment