kanakatam
04-16-2005, 12:27 PM
This program is to input 20 numbers in the range 0 to 100 and display a histogram that shows how many numbers are in the range 0 to 9, 10 to 19, 20 to 29..
public class Histogram extends Applet
{
private int [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55,
61, 63, 70, 76, 84, 91, 95, 98}; // input array
private int [] hist = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // histogram count
public void drawHist()
{
for( int i = 0; i <num.length; i++)
{
hist[num[i] / 10] += 1;
}
}
public void paint (Graphics g)
{
int x = 20;
int y = 20;
int height = 40;
for(int i = 0; i < hist.length; i++)
{
drawHist();
int width = hist[i];
g.fillRect(x, y, width * 10, height);
x = x + 10;
}
}
}
But i see a solid black rectangle that stretches from left to right when the applet window is dragged. It doesn't look like histogram(bar chart). I don't know what am i doing wrong.
Any help appreciated,
Kt
public class Histogram extends Applet
{
private int [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55,
61, 63, 70, 76, 84, 91, 95, 98}; // input array
private int [] hist = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // histogram count
public void drawHist()
{
for( int i = 0; i <num.length; i++)
{
hist[num[i] / 10] += 1;
}
}
public void paint (Graphics g)
{
int x = 20;
int y = 20;
int height = 40;
for(int i = 0; i < hist.length; i++)
{
drawHist();
int width = hist[i];
g.fillRect(x, y, width * 10, height);
x = x + 10;
}
}
}
But i see a solid black rectangle that stretches from left to right when the applet window is dragged. It doesn't look like histogram(bar chart). I don't know what am i doing wrong.
Any help appreciated,
Kt