I am at a complete loss here. I would honestly try this but i don\'t even know w
ID: 3621692 • Letter: I
Question
I am at a complete loss here. I would honestly try this but i don't even know where to start! Also, in my instructions here, I need to pull in Generator.class, and im not sure how to use that, so any guess at that would be so appreciated. But here is the assignment.
Output would be like so...
How many numbers? 100
How many intervals? 10
Histogram
--------------------------------------------------------
1 ****(4)
2 ******(6)
3 ***********(11)
4 *****************(17)
5 **************************(26)
6 *************************(25)
7 *******(7)
8 ***(3)
9 (0)
10 *(1)
--------------------------------------------------------
The program performs the following actions:
Ask the user to enter the number of numbers that will be generated and the number of intervals that will be used in the histogram, and input the two values;
Generate the required number of pseudo-random numbers by using the double[] Generator.getData(int n), which, given the number of numbers to generate, n, returns an array of double of size n containing the generated numbers;
Compute the range of data values (by finding the maximum and the minimum and taking their difference); then compute the width of each equally-sized interval (by dividing the whole range by the number of intervals specified by the user); finally, compute the number of data numbers falling in each interval and store these frequencies in an array of integers;
Output the histogram of the data as displayed in the sample run above: each bar starts with an index (1 through the number of intervals), followed by a number of '*' equal to the number of numbers in that range, followed by the number of '*' in parentheses.
Note: Pay attention to detail. Make your output look like the output in the sample run. However, note that your program must be able to produce the correct histogram for arbitrary data, not just for the specific data generated by Generator.getData().
I need to utilize static methods and arrays to make this, so any kind of other information on those two things would be amazing.
I would greatly appreciate any hints or help on this, so much... Thankyou.
Explanation / Answer
please rate - thanks
I didn't have your class for the random numbers, so I made my own method
import java.util.*;
public class histogram
{
public static void main(String[] args)
{ Scanner in = new Scanner(System.in);
int n,intervals;
System.out.print("How many numbers? ");
n=in.nextInt();
double[]numbers=new double[n];
double range;
System.out.print("How many intervals? ");
intervals=in.nextInt();
numbers =getData( n);
range=getrange(numbers,n);
double size=range/intervals;
int[] hist=new int[intervals];
hist=gethist(numbers,n,size,intervals);
drawhist(intervals,hist);
}
public static void drawhist(int intervals, int hist[])
{int i,j;
System.out.println(" Histogram ---------------------------");
for(i=0;i<intervals;i++)
{System.out.print((i+1)+" ");
for(j=0;j<hist[i];j++)
System.out.print("*");
System.out.println("("+hist[i]+")");
}
}
public static int[] gethist(double numbers[],int n,double size,int intervals)
{int i,j;
int[] h=new int[intervals];
double[]ranges=new double[intervals+1];
double min=numbers[0];
for(i=1;i<n;i++)
{if(numbers[i]<min)
min=numbers[i];
}
ranges[0]=0;
for(i=1;i<=intervals;i++)
ranges[i]=size*i+min;
for(i=0;i<intervals;i++)
h[i]=0;
for(i=0;i<n;i++)
{for(j=1;j<=intervals;j++)
{
if(numbers[i]>ranges[j-1]&&numbers[i]<=ranges[j])
{h[j-1]++;
break;
}
}
}
return h;
}
public static double[] getData(int n)
{ Random generator = new Random();
int i;
double[]numbers=new double[n];
for(i=0;i<n;i++)
numbers[i]=generator.nextDouble();
return numbers;
}
public static double getrange(double num[],int n)
{double max,min;
int i;
max=num[0];
min=num[0];
for(i=1;i<n;i++)
{if(num[i]>max)
max=num[i];
else if(num[i]<min)
min=num[i];
}
return max-min;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.