Hey all,

i have written a short program which outputs a basic Histogram in this form:

(0-25) ***
(26-40) ****
(41-75) **
(76-80) ****
(81-100) **

I'm required to alter my solution so as well as that it also outputs it vertically so
i'm after something like this:
* * *
* *
**
(0-25)(26-40)(41-75) ...etc

Am i right in thinking it would work if i decalre a 2D array and put the asterisks in elements (0,0) (1,0) and (2,0) and then my ranges in (0,1) (1,1) and (2,1) and then printed the array as a whole it would show everything in the right places? Or am i misunderstanding?

and im sorry about the rubbish vertical diagram there i couldnt get it to all line up but i hope you see what i mean.

Recommended Answers

All 8 Replies

That could work. Why don't you try it and see?
It's also possible to do it without the array, for example by printing a row at a time: for each column (a range in your example) you'd print a '*' if there needs to be something there, and a ' ' if not, and maybe put some spacing in between. That seems be simpler to me, but if you're happier with the array solution you should give it a go. Always try the way that seems clearest to you first. You can always scrap it and do it again, and you'll know more from having tried something that didn't work.

That could work. Why don't you try it and see?
It's also possible to do it without the array, for example by printing a row at a time: for each column (a range in your example) you'd print a '*' if there needs to be something there, and a ' ' if not, and maybe put some spacing in between. That seems be simpler to me, but if you're happier with the array solution you should give it a go. Always try the way that seems clearest to you first. You can always scrap it and do it again, and you'll know more from having tried something that didn't work.

I did as you said and i have the program print out the required strings made up of spaces and asterisks in the right places; and it does list the Histogram vertically now. Only trouble is in effect its upside down is there anyway i can either print on the previous line or just get it the other way up?

Good, that's progress. Iterative development: get it closer to right, then correct course and try again.
Now, printing to the previous line would require some sort of curses library, which may or may not be implemented for Java, but would probably be a lot more trouble than you want in any case.
Why don't you try looking at your code and reversing the order in which you print out the lines instead?

Good, that's progress. Iterative development: get it closer to right, then correct course and try again.
Now, printing to the previous line would require some sort of curses library, which may or may not be implemented for Java, but would probably be a lot more trouble than you want in any case.
Why don't you try looking at your code and reversing the order in which you print out the lines instead?

The way my code is printing is using IF statements inside a while loop. Whenever a certain condition is met (in this case when certain counters are either 0 or not) it outputs the appropriate string of characters. I dont see how i could reverse that order as it simply prints whenever that condition is met. If i remember my A-level computing lessons (been a few years now) can't the items in a list be reversed using a stack? or am i just very lost and confused?

Stacks are good for reversing things: put stuff into the stack until you run out of stuff, then pull stuff out and put it on the screen until the stack is empty.

However, I believe that it should be possible to do it straight to the screen. Assuming you have found the maximum value in your data set, and worked out what your scale will be you would start at the top of the scale and work your way down. In your example, for your first row you would print a '*' in any column that gets 5 stars, then in the next row a star in any column that gets 4 or more, and so forth.

Or you could make a stack, if you like.

Stacks are good for reversing things: put stuff into the stack until you run out of stuff, then pull stuff out and put it on the screen until the stack is empty.

However, I believe that it should be possible to do it straight to the screen. Assuming you have found the maximum value in your data set, and worked out what your scale will be you would start at the top of the scale and work your way down. In your example, for your first row you would print a '*' in any column that gets 5 stars, then in the next row a star in any column that gets 4 or more, and so forth.

Or you could make a stack, if you like.

The Stack implementation worked great! Thanks Jon. I'll mark this thread solved. Now how do i go about letting the world know your my new saviour?

The Stack implementation worked great! Thanks Jon. I'll mark this thread solved. Now how do i go about letting the world know your my new saviour?

A billboard will do nicely. Preferably near the Boston Garden - my boss lives right by there, it would be cool if he saw that out his window every morning. :)

A billboard will do nicely. Preferably near the Boston Garden - my boss lives right by there, it would be cool if he saw that out his window every morning. :)

If it's warmer there than London is right now consider me there!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.