-1

I"m having trouble with part D-finding the max and min from the average.Need help being able to calculate the min and max after it has been averaged.

I need to write a program where

  • I get a sample size
  • For each of the 4 trials reads in the data for each sample per trial
  • Prints out each bug per sample in a table format

    // Table header line
    System.out.println("\tSample #\tTrial 1\tTrial 2\tTrial 3\tTrial 4");
    
  • Print out the average for each case AND figure out how closely the trials match with each other by comparing the averages using min and max.

Matching min and max means the trials match exactly –The trials match EXACTLY! The trials concur with each other if the max is less than twice the min – "The trials concur with each other!" Otherwise the differential between the trials is too high and they do not concur with each other – The trials do not concur!

This is what I have so far...

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);


    //---Get the sample size

    System.out.print("Please enter the sample size: ");
    int max = input.nextInt();

    int[]arr0 = new int [max+1];
    int[]arr1 = new int [max+1];
    int[]arr2 = new int [max+1];
    int[]arr3 = new int [max+1];


    int count = 0, total = 0, num;

    {
        System.out.println("Enter numbers for Trial 0");
        for (int j = 0; j < max; j++) {
            System.out.print("Enter sample #" + j + ":");
            num = input.nextInt();
            if (num > 0) {
                arr0[j] = num;
                total += num;

            count++;
            arr0[max] = total/count;
        }

    }
}
    System.out.println("");

    //----------------------------------------

    int count1 = 0, total1 = 0, num1;

    {
        System.out.println("Enter numbers for Trial 1");
        for (int j = 0; j < max; j++) {
            System.out.print("Enter sample #" + j + ":");
            num1 = input.nextInt();
            if (num1 > 0) {
                arr1[j] = num1;
                total1 += num1;

            count1++;
            arr1[max] = total1/count1;
            }
        }

    }

    System.out.println("");

    //----------------------------------------

    int count2 = 0, total2 = 0, num2;

    {
        System.out.println("Enter numbers for Trial 2");
        for (int j = 0; j < max; j++) {
            System.out.print("Enter sample #" + j + ":");
            num2 = input.nextInt();
            if (num2 > 0); {
                arr2[j] = num2;
                total2 += num2;


                count2++;
                arr2[max] = total2/count2;
            }

        }
    }

    System.out.println("");

    //----------------------------------------

    int count3 = 0, total3 = 0, num3;

    {
        System.out.println("Enter numbers for Trial 3");
        for (int j = 0; j < max; j++) {
            System.out.print("Enter sample #" + j + ":");
            num3 = input.nextInt();
            if (num3 > 0); {
                arr3[j] = num3;
                total3 += num3;

                count3++;
                arr3[max] = total3/count3;

            }

        }

    }

    System.out.println("");


    // Table header line

    System.out.println("\tSample    #\tTrial    1\tTrial    2\tTrial    3\tTrial    4"); 
    for (int j = 0; j < max; j++) {
        System.out.print("\t" + j + "\t\t" + arr0[j] + "\t\t" + arr1[j]);
        System.out.println("\t\t" + arr2[j] + "\t\t" + arr3[j]);
    }


    //System.out.println("");   

    System.out.println("\t----------------------------------------------------------------------"); 

    //---Print out the average for each trial



    System.out.print("Average:\t\t" + arr0[max] + "\t\t" + arr1[max]);
    System.out.println("\t\t" + arr2[max] + "\t\t" + arr3[max]);



    // Figure out how closely the trials match with each other by comparing the averages using min and max

    System.out.println(" ");

    {
        if (arr1[max] == arr2[max]) {
            System.out.print("** The trials match EXACTLY! **");
        }
        if (arr1[max] > arr2[max]) {
            System.out.print("** The trials concur with each other! **");
        }
        if (arr1[max] < arr2[max]) {
            System.out.print("** The trials do NOT concur! **");
        }


    }


}

}

2

2 Answers 2

0

I think you might want to look into your 'min' variable. As far as I can tell you haven't declared it. You access it once (around line 130ish) but nothing is ever written to it.

2
  • That's another thing I'm not quite sure what to declare it as since I'm trying to make it the smallest number of the overall averages. Dec 6, 2013 at 7:57
  • Declare it as a double and make it a larger value than anything that would be input. When in doubt just give it the value Double.MAX_VALUE.
    – rstone5
    Dec 6, 2013 at 8:30
0

I don't understand the logic of comparing the Trials...

I did create an example, where I end up with 5 arrays you can use for your comparisson logic:

// Array to store the trials
int[][] trials = new int[numTrials][num];
// Array to store the stats (min, max, avg)
float[][] stats = new float[numTrials][3];

// Results
float[] minima = new float[numTrials]; // contains the minimum of all trials
float[] maxima = new float[numTrials]; // contains maxima of all the trials
float[] averag = new float[numTrials]; // averages of all the trials

If you explain the logic of comparing the runs, I can help you. Here's what I have now to generate the 5 arrays you seem to be needing, maybe it helps you as well:

public static void main(String[] args) {

    // Number of Trials
    int numTrials = 4;

    //---Get the sample size
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter the sample size: ");
    int num = input.nextInt();
    input.nextLine();

    // Create explanation txt
    StringBuilder explain = new StringBuilder();
    for (int j = 0; j < num; j++) {
        if (explain.length() <= 0)
            explain.append("" + j);
        else
            explain.append("," + j);
    }

    // Array to store the trials
    int[][] trials = new int[numTrials][num];

    // Array to store the stats (min, max, avg)
    float[][] stats = new float[numTrials][3];

    // For the number of trials
    for (int i = 0; i < numTrials; i++) {

        // Define min,max, avg
        float min = Integer.MAX_VALUE;
        float max = Integer.MIN_VALUE;
        float avg = 0;

        // Ask for a trial run
        System.out.println("Enter numbers for Trial " + (i + 1) + " (e.g. " + explain.toString() + "):");
        String line = input.nextLine();
        String[] nums = line.split(",");

        /**
         * Extra check, to make sure user does not enter something like:
         * 'hallo'
         */
        while (nums.length != num) {

            // Tell User we did not receive the numbers we expected, and let them try again
            System.err.println("Invalid input. You have to enter " + num + " number, but we received only " + nums.length + " ! please try again");
            System.out.println("Enter numbers for Trial " + (i + 1) + " (e.g. " + explain.toString() + "):");

            // Read the input
            line = input.nextLine();

            // Split input on ',' character
            nums = line.split(",");
        }

        for (int n = 0; n < nums.length; n++) {
            int value = 0;
            try {
                // Get the input as a string
                String userInput = nums[n];

                // Strip any spaces etc from the string
                userInput = userInput.trim().replace(" ", "");

                // Try to parse the String to an integer
                value = Integer.parseInt(userInput);
            } catch (Exception e) {
            }

            // Minimum
            if (value < min)
                min = value;
            stats[i][0] = min;

            // Maximum
            if (value > max)
                max = value;
            stats[i][1] = max;

            // Avg
            avg = ((avg * n) + value) / (n + 1);
            stats[i][2] = avg;

            trials[i][n] = value;
        }
    }

    System.out.println("");

    // Table header line
    System.out.println("\tSample    #\tTrial    1\tTrial    2\tTrial    3\tTrial    4");
    for (int n = 0; n < num; n++) {
        System.out.print("\t" + n);
        for (int i = 0; i < numTrials; i++) {
            System.out.print("\t\t" + trials[i][n]);
        }
        System.out.println();
    }

    System.out.println("\t----------------------------------------------------------------------");

    String[] txts = { "min", "max", "avg" };
    for (int n = 0; n < txts.length; n++) {
        System.out.print("\t" + txts[n]);
        for (int i = 0; i < numTrials; i++) {
            //System.out.print("\t\t" + stats[i][n]);
            System.out.printf("\t\t%.2f", stats[i][n]);
        }
        System.out.println();
    }

    System.out.println("\t----------------------------------------------------------------------");

    float[] minima = new float[numTrials];
    float[] maxima = new float[numTrials];
    float[] avergs = new float[numTrials];
    for (int n = 0; n < numTrials; n++) {
        minima[n] = stats[n][0];
        maxima[n] = stats[n][1];
        avergs[n] = stats[n][2];
    }

    // Now, you can easily loop through all minima to compare
}

Here's the output of running the above code:

Please enter the sample size: 2
Enter numbers for Trial 1 (e.g. 0,1):
1,2
Enter numbers for Trial 2 (e.g. 0,1):
2,3
Enter numbers for Trial 3 (e.g. 0,1):
0,2
Enter numbers for Trial 4 (e.g. 0,1):
1,2

    Sample    # Trial    1  Trial    2  Trial    3  Trial    4
    0       1       2       0       1
    1       2       3       2       2
    ----------------------------------------------------------------------
    min     1.00        2.00        0.00        1.00
    max     2.00        3.00        2.00        2.00
    avg     1.50        2.50        1.00        1.50
    ----------------------------------------------------------------------

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.