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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -