andrewsco
10-07-2004, 11:19 PM
I have a project that i need a bit of help with. I have done the first bit, but am unsure on the second bit.
Here is the problem:
Exercises 3 and 4. Write a Java program to
1. Read three integers, say n, bottom and top, from the input stream;
2. Read n ‘double’ numbers and count how many of them fall in each of the intervals (-,bottom), (bottom,bottom+1), ..., (top-1,top), (top,); store these counts (frequencies) in an array with top-bottom+2 elements;
3. Display the observed frequencies in a histogram, using an appropriate number of ‘*’ for each (horizontal) bar. For example, if n is 20, bottom is 1, top is 4, and the observed frequencies are 5, 0, 8, 3, 4, the histogram may appear as follows (you can decide how to label the bars, and what title or caption to include):
(-inf,1) | *****
(1,2) |
(2,3) | ********
(3,4) | ***
(4,inf) | ****
Exercise 3 consists of steps 1 and 2 above (print the resulting array); exercise 4 is step 3.
import java.io.*;
import javagently.*;
public class Frequency
{
public static void main() throws IOException
{
Stream input = new Stream (System.in);
int n = 0; //Stores how many numbers to be inputted
int bottom = 0; //stores min number
int top = 0; // stores max number
int i = 0; // used for loop variables
double newestNum = 0; // variable for temp. storing doubles as they are read in
System.out.print("How many numbers would you like to input: ");
n = input.readInt();
System.out.print("Please enter the upper bound: ");
top = input.readInt();
System.out.print("Please enter the lower bound: ");
bottom = input.readInt();
if(bottom>top)
{
System.out.print("Value for bottom must be less than that of top");
System.exit(0);
}
int [] freq = new int [top-bottom+2]; //an array to store all possibilites for graph
for (i = 0; i < n; i++) // will read in n numbers
{
System.out.print("Please enter a number: ");
newestNum = input.readDouble();
if (newestNum < bottom)
{
freq [0]++;
}
else if (newestNum >= top)
{
freq [top-bottom+1]++;
}
else
{
freq [(int)newestNum-bottom+1]++; //If you dont know how this works write out an example with say bottom = 1, top = 4 }
}
for (i=0; i < freq.length; i++)
System.out.print(freq[i] + " ");
}
}
I'm not too sure on how to get the intervals to be printed? I was thinking on similar lines to above, ie having a for loop, with the array initialized to [0], if it is print (-inf, bottom) if its [top-bottom+1] then it is (top, inf) but I cant get my head around the middle bit. Would I need two vaklues, say x and y, and need to compare them, or is there an easier way? Some code would be VERY appreciated, as I am tearing my hair out!
Andy
Here is the problem:
Exercises 3 and 4. Write a Java program to
1. Read three integers, say n, bottom and top, from the input stream;
2. Read n ‘double’ numbers and count how many of them fall in each of the intervals (-,bottom), (bottom,bottom+1), ..., (top-1,top), (top,); store these counts (frequencies) in an array with top-bottom+2 elements;
3. Display the observed frequencies in a histogram, using an appropriate number of ‘*’ for each (horizontal) bar. For example, if n is 20, bottom is 1, top is 4, and the observed frequencies are 5, 0, 8, 3, 4, the histogram may appear as follows (you can decide how to label the bars, and what title or caption to include):
(-inf,1) | *****
(1,2) |
(2,3) | ********
(3,4) | ***
(4,inf) | ****
Exercise 3 consists of steps 1 and 2 above (print the resulting array); exercise 4 is step 3.
import java.io.*;
import javagently.*;
public class Frequency
{
public static void main() throws IOException
{
Stream input = new Stream (System.in);
int n = 0; //Stores how many numbers to be inputted
int bottom = 0; //stores min number
int top = 0; // stores max number
int i = 0; // used for loop variables
double newestNum = 0; // variable for temp. storing doubles as they are read in
System.out.print("How many numbers would you like to input: ");
n = input.readInt();
System.out.print("Please enter the upper bound: ");
top = input.readInt();
System.out.print("Please enter the lower bound: ");
bottom = input.readInt();
if(bottom>top)
{
System.out.print("Value for bottom must be less than that of top");
System.exit(0);
}
int [] freq = new int [top-bottom+2]; //an array to store all possibilites for graph
for (i = 0; i < n; i++) // will read in n numbers
{
System.out.print("Please enter a number: ");
newestNum = input.readDouble();
if (newestNum < bottom)
{
freq [0]++;
}
else if (newestNum >= top)
{
freq [top-bottom+1]++;
}
else
{
freq [(int)newestNum-bottom+1]++; //If you dont know how this works write out an example with say bottom = 1, top = 4 }
}
for (i=0; i < freq.length; i++)
System.out.print(freq[i] + " ");
}
}
I'm not too sure on how to get the intervals to be printed? I was thinking on similar lines to above, ie having a for loop, with the array initialized to [0], if it is print (-inf, bottom) if its [top-bottom+1] then it is (top, inf) but I cant get my head around the middle bit. Would I need two vaklues, say x and y, and need to compare them, or is there an easier way? Some code would be VERY appreciated, as I am tearing my hair out!
Andy