0

I need to develop a java program that asks the user to enter some integers and find the largest, and smallest number, and the average of those numbers. Then, divides the set of array into a number of sub-intervals that the user specifies, then it generates a boundary points each has a length of sub-interval width..

The problem is that I need to create a frequency:

ex: Interval:

14.5-16.5

Frequency: 1 (here it should shows how many integers belong to this boundaries)

Interval:

16.5-18.5

Frequency: 4 and so on.

Here is the code I have so far, almost done except finding the frequency of each boundary..

import java.util.Scanner;
public class Sta
{
    public static void main(final String args[])
    {
        final Scanner input = new Scanner(System.in);

        int num=0;
        int range=0;
        int subnum;
        int subwid;

        System.out.print("How many numbers do you want to enter: ");
        num=input.nextInt();

        final int array[]=new int[num];

        System.out.print("Enter the numbers now: ");
        for(int i=0; i<array.length; i++)
        {
            array[i]=input.nextInt();
        }

        System.out.print("These are the numbers you entered:\n");
        printArray(array);

        int smallest=array[0];
        int largest=array[0];

        for (final int element : array) {
            if(element>largest) {
                largest=element;
            } else if(element<smallest) {
                smallest=element;
            }
            range=largest-smallest;
        }
        System.out.printf("Largest is %d\n",largest);
        System.out.printf("Smallest is %d\n",smallest);
        System.out.printf("Range is %d\n",range);

        System.out.print("Enter the number of subinterval: ");
        subnum=input.nextInt();

        subwid=range/subnum;
        System.out.printf("The width of subinterval is %d\n", subwid);

        /* this part should find the boundaries and find the elements that fall between
           the each two boundaries */
        for(double boundary=smallest-.5; boundary <=largest+.5; boundary +=subwid)
        {
        System.out.printf("Boundaries are %.1f\n",boundary);

        for(int element=0; element<array.length; element++)
          {
            if(element>=boundary)
              {
            System.out.printf("f=%d\n",element);
              }
          }
         }
    }

    public static void printArray(final int arr[])
    {

        for (final int element : arr) {
            System.out.print(element + "\n");
        }
    }

}

The question is how do I find the frequencies as in the above table example ??

2
  • Un-indented code is unreadable. I indented it for you. Oct 19, 2011 at 16:39
  • 2
    If you've found the answer yourself, you should enter what you've found as an answer not edit out the question. Then accept your own answer by clicking the check mark. Oct 23, 2011 at 6:32

1 Answer 1

2

Here are some options:

  • Nested Loops: For each range, count the number of numbers that fall in that range.
  • 'new int[subnum]'. In one pass, count the number of items in each range. Consider using division and truncation.
  • Sort the numbers. Count how many occur before each boundary.
1
  • 3
    @AishaS: That's not how it works. Please edit your post, show us your best attempt and tell us how it's not working. Oct 20, 2011 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.