Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex5 {
- // Write a Java program to compute the average value of an array of integers
- // except the largest and smallest values.
- public static void main(String[] args) {
- int[] arr = { 2, 100, 1, 20, 9 };
- int max = arr[0];
- int min = arr[0];
- int sum = 0;
- for (int i = 0; i < arr.length; i += 1) {
- sum += arr[i];
- if (arr[i] > max)
- max = arr[i];
- else if (arr[i] < min) {
- min = arr[i];
- }
- }
- double avg = (double) (sum - max - min) / (arr.length - 2);
- System.out.println("avg = " + avg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement