0

My question is about discarding the largest and smallest number when my program calculates an arithmetic mean.

This is my code sample:

public static void main(String[] args) {
    int k;
    int r;
    int thelargest;
    int thesmallest;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the list of number : ");
    String input2 = input.nextLine();

    String[] numbers = input2.split(" ");

    int[] result = new int[numbers.length];
    for (r = 0; r < numbers.length; r++) {
        result[r] = Integer.parseInt(numbers[r]);

    }

    for (k = 0; k < result.length; k++) {
        System.out.print("");
        System.out.println(result[k]);
    }

    System.out.println(" LargestNumber :  " + TheLargestNumber(result));
    System.out.println(" SmallestNumber :  " + TheSmallestNumber(result));
    thelargest = TheLargestNumber(result);
    thesmallest = TheSmallestNumber(result);
    System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));

}

public static int TheSmallestNumber(int[] series) {
    int thesmallest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] < thesmallest) {

            thesmallest = series[i];
        }
    }
    return thesmallest;
}

public static int TheLargestNumber(int[] series) {
    int thelargest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] > thelargest) {

            thelargest = series[i];
        }
    }
    return thelargest;
}

public static float AirthmeticMean(int[] result) {
    int sum = 0;
    for (int i = 0; i < result.length; i++) {
        sum += result[i];
    }
    return (float) sum / result.length;
}

I tried to find the way and I wrote this sample but I don't know how to embed this code sample:

         for (int i = 0; i < result.length; i++) {
        if (series[i] != thesmallest && series[i] != thelargest) {
            total = total + seriess[i];
        }
    }

Will this code sample be helpful to me?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • 1
    When you iterate through the loop adding numbers, check for the largest and smallest number. Then after the loop is complete, subtract those numbers out of your sum. Easy as PI. – Hovercraft Full Of Eels May 05 '13 at 17:49
  • can you open it ?because I don't understand what to do .Showing on a code sample will be good for me .@HovercraftFullOfEels –  May 05 '13 at 17:51
  • 1
    @Hovercraft Full Of Eels: This doesn't work if the largest or smallest number occurs multiple times. – Barry NL May 05 '13 at 17:51
  • TheSmallestNumber better to have function names start with lower case like theSmallestNumber – tgkprog May 05 '13 at 18:02
  • http://jtf.acm.org/tutorial/Introduction.html this is ACM lib ? – tgkprog May 06 '13 at 03:09

3 Answers3

0

The code sample you've got:

for (int i = 0; i < result.length; i++) {
    if (series[i] != thesmallest && series[i] != thelargest) {
        total = total + seriess[i];
    }
}

is almost OK. Almost, because you retrieve the length of the array named result in the for-loop and access elements of array series.

You can use this code in the following way. Extend AirthmeticMean with two parameters theSmallest and theLargest and keep track of the number of summed elements:

public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
    int sum = 0;
    int numElements = 0;
    for (int i = 0; i < result.length; i++) {
        if (result[i] != theSmallest && result[i] != theLargest) {
            sum += result[i];
            numElements++;
        }
    }
    return (float) sum / numElements;
}

EDIT: added numElements.

harpun
  • 4,022
  • 1
  • 36
  • 40
  • The last proposal doesn't work if the largest or smallest number occurs multiple times. (Poster edit: it had a solution where it subtracted the smallest and largest number out of the sum once.) – Barry NL May 05 '13 at 17:52
  • 1
    Thank you my friend .It works.Can I ask you one more last thing ? Can I run this programme with using ACM libary(ConsoleProgram)?Do you have any information about this ? –  May 05 '13 at 17:58
  • @BarryNL I removed this suggestion. It depends on whether you discard the smallest and largest numbers at all or only once. I'd say it's a matter of definition :) – harpun May 05 '13 at 17:59
  • @mertha sorry, I can't help you with ACM libraries. – harpun May 05 '13 at 18:01
  • array list -> remove all occurrences of smallest and largest – tgkprog May 05 '13 at 18:03
  • @tgkprog what does mean "array list -> remove all occurrences of smallest and largest"? Do you have any idea about writing this program with ACM libary(consoleprogram)? –  May 05 '13 at 18:09
  • @harpun you are not adding the smallest and largest but your are dividing using the total length? so if set was 2,3,5 the answer would be 3/3 = 1? it should be 3/1 = 3 – tgkprog May 06 '13 at 03:01
  • @mertha no i do not know ACM do you have a link to it? – tgkprog May 06 '13 at 03:01
  • @tgkprog: you're right. I corrected the code. The ACM issue was discussed in another question: http://stackoverflow.com/questions/16387867/how-to-write-my-java-programme-with-acm-libraryconsoleprogram – harpun May 06 '13 at 16:38
0

Just before your

System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));

write

thenewmean = (AirthmeticMean(result)*result.length - thesmallest - thelargest)/(result.length-2)

and then print thenewmean

System.out.println("The Arithmetic Mean : " + thenewmean);

You don't need to write your

for (int i = 0; i < result.length; i++) {
    if (series[i] != thesmallest && series[i] != thelargest) {
        total = total + seriess[i];
    }
}

code anywhere. Even then if you wish to use your own code, then use it in your AirthmeticMean() function

Jayant
  • 152
  • 9
0

Need to keep track of count as well as sum if removing highest and smallest

public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
    int sum = 0;
    int cnt = 0;
    for (int i = 0; i < result.length; i++) {
            if (result[i] != theSmallest && result[i] != theLargest) {
                sum += result[i];
                cnt++;
            }
    }
    return (float) sum / cnt;
}
tgkprog
  • 4,493
  • 4
  • 41
  • 70