programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Displaying a histogram using input from an array

Ranch Hand
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:
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

Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:

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
Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:
(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
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:

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

 You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too! the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising