Advertisement
mmayoub

School, 07.09.2017, Ex5, find average without max and min

Sep 7th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1.  
  2. public class Ex5 {
  3.     // Write a Java program to compute the average value of an array of integers
  4.     // except the largest and smallest values.
  5.     public static void main(String[] args) {
  6.         int[] arr = { 2, 100, 1, 20, 9 };
  7.  
  8.         int max = arr[0];
  9.         int min = arr[0];
  10.         int sum = 0;
  11.  
  12.         for (int i = 0; i < arr.length; i += 1) {
  13.             sum += arr[i];
  14.  
  15.             if (arr[i] > max)
  16.                 max = arr[i];
  17.             else if (arr[i] < min) {
  18.                 min = arr[i];
  19.             }
  20.         }
  21.  
  22.         double avg = (double) (sum - max - min) / (arr.length - 2);
  23.  
  24.         System.out.println("avg = " + avg);
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement