﻿ Java - Average value of an array except the largest, smallest

# Java: Compute the average value of an array of integers except the largest and smallest values

## Java Array: Exercise-29 with Solution

Write a Java program to compute the average value of an array of integers except the largest and smallest values.

Pictorial Presentation: Sample Solution:

Java Code:

``````// Import the java.util package to use utility classes, including Arrays.
import java.util.*;

// Import the java.io package to use input and output classes.
import java.io.*;

// Define a class named Exercise29.
public class Exercise29 {
// The main method for executing the program.
public static void main(String[] args) {
// Declare and initialize an array of integers.
int[] array_nums = {5, 7, 2, 4, 9};

// Print the original array.
System.out.println("Original Array: " + Arrays.toString(array_nums));

// Initialize variables for maximum, minimum, and sum.
int max = array_nums;
int min = array_nums;
float sum = array_nums;

// Use a loop to find the maximum, minimum, and calculate the sum of the array elements.
for (int i = 1; i < array_nums.length; i++) {
sum += array_nums[i];
if (array_nums[i] > max)
max = array_nums[i];
else if (array_nums[i] < min)
min = array_nums[i];
}

// Calculate the average value of the array except for the largest and smallest values.
float x = ((sum - max - min) / (array_nums.length - 2));

// Print the calculated average value with two decimal places.
System.out.printf("Compute the average value of an array of integers except the largest and smallest values: %.2f", x);

// Print a new line.
System.out.print("\n");
}
}
```
```

Sample Output:

```
Original Array: [5, 7, 2, 4, 9]
Compute the average value of an array of integers except the largest an
d smallest values: 5.33
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿