# Landscaped Histogram

• 10-19-2012, 10:07 AM
SoullessWhale
Landscaped Histogram
So I have an array of 100 numbers and I need to make it do this..

Code:

```with grades:  0, 2, 2, 2, 5, 10, 10, 11, 11, 12, 30, 100   *   *      ** * *  *    ***                *                                                                    * +---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+           1        2        3        4        5        6        7        8        9        1 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890```
thats an example with less numbers.
any hints?
• 10-19-2012, 10:08 AM
SoullessWhale
Re: Landscaped Histogram
and I already made the scale I just need to do the stars. I cant get them to print correctly..
• 10-19-2012, 10:13 AM
penguinCoder
Re: Landscaped Histogram
Well, obviously you are going to need an array of integers with a 100 different indexes. The easiest way to load that array is going to be using the following method:
X = one of your numbers
Code:

`array[x]++;`
Just keep looping that to insert all of your data.

Displaying the data is going to be a little trickier, but should use a similar method.

edit--
Don't know how I missed the fact that you said you already had the array of number, made my post seem a bit demeaning.. Was not meant that way..
• 10-19-2012, 10:18 AM
SoullessWhale
Re: Landscaped Histogram
I have started doing that and have it where it prints in portrait. I know its going to be something similar Im just having difficulties making it work.

Code:

```    public void printHistogramLandscape () {         int[] modeArray = new int[MAX_VALUE + 1];                 for (int num : numberArray)             modeArray[num]++;         for (int i = 0; i < modeArray.length; i++) {             if (modeArray[i] > 0) {                 for (int j = 0; j < modeArray[i]; j++)                     System.out.printf("*");                 System.out.println();             }         }         System.out.print("+");         for(int i = 0; i < 10; i++){             System.out.print("---------+");         }         System.out.println();         System.out.print(" ");         for(int i = 1; i <= 10; i++){             System.out.print("        " + i);         }         System.out.println();         for(int i = 0; i < 10; i++){             System.out.print("0123456789");         }         System.out.println("0");```
• 10-19-2012, 10:19 AM
penguinCoder
Re: Landscaped Histogram
The easiest way I see for displaying the horizontal histogram, would be to loop a space one less then the index, and then at the end of the loop display a star. Something along the lines of that.

edit--
Alright that wouldn't work. Lol, this it a little trickier then I originally thought.. Let me think for a second..
• 10-19-2012, 10:23 AM
SoullessWhale
Re: Landscaped Histogram
But then I run into the issue of if there are say, 3 2's and 3 4's it will do 2 spaces and print a * then move on to the next line
• 10-19-2012, 10:29 AM
SoullessWhale
Re: Landscaped Histogram
nothing explicitly says we cant but we havent learned it in class so Id say no haha
• 10-19-2012, 10:51 AM
penguinCoder
Re: Landscaped Histogram
I believe that you are going to have to start by using a single loop and a single comparison to find the largest occurances number. And then from there, you would be able to start displaying them from the top. If a number in the array has an occurance that high, then you are going to have to loop a space the number of times between either 0 and 1 less then it, or between the last number that had that number and 1 less then itself and then display the asterisk. Then repeat up until you reach 100, and then println it.

Then just repeat with largest number minus 1.

Hopefully that makes sense, as it is starting to get a little late over here. I believe that would work though, someone else will probably come along and post something a little more efficient..
• 10-19-2012, 11:19 AM
SoullessWhale
Re: Landscaped Histogram
only problem with that plan is the largest occurrence of a number happens like 5 times but that happens with two numbers ill have the same problem as before.
• 10-19-2012, 11:44 AM
Tolls
Re: Landscaped Histogram
This is off the top of my head, but it looks to me like you want to be able to create a line based on the number of entries that line represents.
You'll be counting down, so the example in the OP will count down from 3 (which is the largest number of values a single column has).
Then for each line you want to see what values have the count <= the row you are trying to print.

This implies an array of 100 ints.
Each index represents a value on the x axis, and each value represents a value on the y axis.
Loops over the array, if the value is > the current row then print a '*' else print a ' '.
Code:

```// note you may not need the values parameter since it'll probably be an attribute of the class public void println(int currentRow, int[] values) {   for each value       if value >= currentRow         print *       else         print space   new line }```
As I say this is off the top of my head, so I may have got something arse about face.