aspose file tools*
The moose likes Beginning Java and the fly likes Displaying a histogram using input from an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Displaying a histogram using input from an array" Watch "Displaying a histogram using input from an array" New topic
Author

Displaying a histogram using input from an array

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Hello folks,
I am currently working on a problem that inputs 20 numbers into an array and the numbers being in the range of 0 to 100. I had to calculate smallest, largest, mean and average of all the numbers in this array which i was able to do without any problem. But the second half of the problem is to display a histogram to show how many numbers are in the ranges of 0 to 9, 10 to 19 etc., This is a class assignment and i did read the instructions of java ranch. So i just need a little head start and not expecting anyone to code for me. This is how far i have gotten with the code.


I am lost in figuring out how to do the histogram for this problem. Any help appreciated for letting me know how to approach this portion of the problem,
Thanx,
kt
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 989
    
    1
Make the histogram in two steps:
1. Collect the numbers into some data structure (most likely an array).
2. Output the results.

Are you supposed to use something like System.out.println() to output a row of asterisks for each range, or do you have to do something more graphical?

Ryan
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Ryan McGuire:
Make the histogram in two steps:
1. Collect the numbers into some data structure (most likely an array).
2. Output the results.

Are you supposed to use something like System.out.println() to output a row of asterisks for each range, or do you have to do something more graphical?

Ryan


Thank you Ryan. I might have to use an array to collect the range say 0 to 9, 10 to 19 etc., But i am very recently introduced to arrays and i don't know how to initialize arrays for a range of numbers.
I am thinking like this, but not sure
private int [] output = {0 - 9, 10 - 19, 20 - 29, 30 - 39, 40 - 49, 50 - 59, 60 - 69, 70 - 79, 80 - 89, 90 - 99};
Then if this format is right i have to figure out where my 20 numbers that i have inputed through another array exists, that is in what range. I have to use filled rectangles to get a histogram that displays how many numbers are in the range 0 to 9, 10 to 19 etc.,
How would i check what number is in what range. I know the procedure but not sure of what to do.
Help appreciated..
kanaka
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 989
    
    1
(edit: remove quote)

That array declaration may cause you some trouble. 0-9 = -9. 10-19 = -9, etc. So you'll end with a ten position array with each position initialized to negative nine.

I think it would be better if you initialized your array to all zeros. And then go through your list of intput numbers and increment one of the ints in output.

Given any input number in the range [0, 99], can you give me an expression or a method that determines which int in output to increment? For example, if you see a 22, you want to increment output[2]. (Make sure the expression or method works for input numbers that are near the ends of your ranges, such as 29 and 30.)

Ryan
[ April 15, 2005: Message edited by: Ryan McGuire ]
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Ryan McGuire:
(edit: remove quote)

That array declaration may cause you some trouble. 0-9 = -9. 10-19 = -9, etc. So you'll end with a ten position array with each position initialized to negative nine.

I think it would be better if you initialized your array to all zeros. And then go through your list of intput numbers and increment one of the ints in output.

Given any input number in the range [0, 99], can you give me an expression or a method that determines which int in output to increment? For example, if you see a 22, you want to increment output[2]. (Make sure the expression or method works for input numbers that are near the ends of your ranges, such as 29 and 30.)

Ryan

[ April 15, 2005: Message edited by: Ryan McGuire ]


Thanx Ryan,
I added the following, no compilation errors but cannot see any histogram output. Couldn't figure out what am i doing wrong. Here is the code.


I would appreciate any help.
thanx,
kt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying a histogram using input from an array
 
Similar Threads
No compilation error, but not the required output
please help figure out why the standard deviaion is not giving the correct output
Problems reading from a .txt file
standard deviation
How to get a method to call a method