AhmedO
AhmedO

Reputation: 165

How to print number of stars corresponding to some numbers vertically?

i am trying to make a histogram to be displayed vertically , my output gives incorrect pattern.

input :

unqNums = [0.0, 2.0, 1.0, 5.0]
repeated = [2, 2, 1, 1]

output:

*    *
*    *    *    *
0.0, 2.0, 1.0, 5.0

my code:

 System.out.println(Arrays.toString(gradesNoRepead));
 //[0.0, 2.0, 1.0, 5.0]
 System.out.println(Arrays.toString(repeatedVal));
 //[2, 2, 1, 1]
//getting the max rep
        int mxRep = Main.getMax(repeatedVal);
        for(int i = mxRep; i > 0; --i){
            for(int l=0; l<gradesNoRepead.length; ++l){
        
                System.out.print((gradesNoRepead[l] >= i) ? " * " : "  ");
            }
            System.out.println();
        }
        for (int m = 0; m < gradesNoRepead.length; m++) {
            System.out.print(" " + gradesNoRepead[m] + " ");
    }
    

Upvotes: 0

Views: 230

Answers (1)

Nick
Nick

Reputation: 147196

Your problem is that in your loop where you are outputting the * values, you are iterating over and testing the values in the wrong array (gradesNoRepead), you should be using repeatedVal:

for(int l=0; l<repeatedVal.length; ++l){
    System.out.print((repeatedVal[l] >= i) ? "  *  " : "     ");
}

Note that you also need some more spaces in the output strings to make them match properly with the width of the gradesNoRepead values.

Upvotes: 1

Related Questions