Results 1 to 10 of 10
  1. #1
    SoullessWhale is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Landscaped Histogram

    So I have an array of 100 numbers and I need to make it do this..

    Java 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?

  2. #2
    SoullessWhale is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: Landscaped Histogram

    and I already made the scale I just need to do the stars. I cant get them to print correctly..

  3. #3
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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
    Java 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..
    Last edited by penguinCoder; 10-19-2012 at 09:16 AM.
    Catching exceptions is for communists

  4. #4
    SoullessWhale is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default 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.

    Java 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");

  5. #5
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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..
    Last edited by penguinCoder; 10-19-2012 at 09:29 AM.
    Catching exceptions is for communists

  6. #6
    SoullessWhale is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default 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

  7. #7
    SoullessWhale is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default Re: Landscaped Histogram

    nothing explicitly says we cant but we havent learned it in class so Id say no haha

  8. #8
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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..
    Catching exceptions is for communists

  9. #9
    SoullessWhale is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Default 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. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default 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 ' '.
    Java 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. getting a rbg histogram from an image
    By aianta in forum New To Java
    Replies: 2
    Last Post: 10-01-2012, 10:59 AM
  2. Help with histogram
    By Conan in forum New To Java
    Replies: 5
    Last Post: 11-02-2011, 03:51 AM
  3. need help with name histogram
    By cachico12 in forum New To Java
    Replies: 1
    Last Post: 05-09-2011, 08:27 AM
  4. histogram
    By little_man in forum New To Java
    Replies: 4
    Last Post: 11-13-2010, 01:38 AM
  5. How to create a Histogram
    By Devilsfutbol17 in forum New To Java
    Replies: 4
    Last Post: 06-04-2008, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •