Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying a histogram using input from an array

 
kanaka tam
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1048
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1048
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic