kanakatam
04-15-2005, 11:13 AM
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, 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.
import java.awt.*;
import java.applet.*;
public class ArrayTest 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};
private int max, min, sum;
private float average;
public void largestSmallest()
{
max = -1;
min = 101;
for (int i = 0; i < num.length; i++)
{
if (max < num[i])
{
max = num[i];
}
if (min > num[i])
{
min = num[i];
}
}
}
public void sumAverage()
{
sum = 0;
average = 0.0f;
for (int i = 0; i < num.length; i++)
{
sum = sum + num[i];
average = (float)(sum) / num.length;
}
}
public void paint (Graphics g)
{
largestSmallest();
sumAverage();
g.drawString("The largest number in this array is: " + max, 30, 50);
g.drawString("The smallest number in this array is: " + min, 30, 80);
g.drawString("The sum of the numbers in this array is: " + sum, 30, 110);
g.drawString("The mean of the numbers in this array is: " + average, 30, 140);
}
}
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
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, 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.
import java.awt.*;
import java.applet.*;
public class ArrayTest 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};
private int max, min, sum;
private float average;
public void largestSmallest()
{
max = -1;
min = 101;
for (int i = 0; i < num.length; i++)
{
if (max < num[i])
{
max = num[i];
}
if (min > num[i])
{
min = num[i];
}
}
}
public void sumAverage()
{
sum = 0;
average = 0.0f;
for (int i = 0; i < num.length; i++)
{
sum = sum + num[i];
average = (float)(sum) / num.length;
}
}
public void paint (Graphics g)
{
largestSmallest();
sumAverage();
g.drawString("The largest number in this array is: " + max, 30, 50);
g.drawString("The smallest number in this array is: " + min, 30, 80);
g.drawString("The sum of the numbers in this array is: " + sum, 30, 110);
g.drawString("The mean of the numbers in this array is: " + average, 30, 140);
}
}
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