Reputation: 1
was looking to find the trimmed average of an array but am stumped on how to implement it using a method. Please shoot any ideas on how to implement and get the actual trimmed average using a method. This is all I have at the moment. TIA!
public static void main(String[] args) {
// Program 1: Average, Min, Max, Trimmed Average of Array
Scanner in = new Scanner(System.in);
int array[] = new int[] {88,48,96,85,97,68,97};
// Calling getMax() method for getting max value
int max = getMax(array);
System.out.println("Maximum: " + max);
// Calling getMin() method for getting min value
int min = getMin(array);
System.out.println("Minimum: " + min);
// calling getAvg() method for getting avg value
double avg = getAvg(array);
System.out.println("Average: " + avg);
// calling getMean() method for getting trimmed mean value
double mean = getTrim(array);
System.out.println("Trimmed mean: " + mean);
}
// Method for getting the maximum value
public static int getMax(int[] inputArray) {
int maxValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] > maxValue) {
maxValue = inputArray[i];
}
}
return maxValue;
}
// Method for getting the minimum value
public static int getMin(int[] inputArray) {
int minValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] < minValue) {
minValue = inputArray[i];
}
}
return minValue;
}
//Method for getting the avg value
public static double getAvg(int[] inputArray) {
double total = 0;
double avg;
for(int i=0; i<inputArray.length; i++){
total = total + inputArray[i];
}
return avg = total / inputArray.length;
}
//Method for getting the trimmed mean value
public static double getTrim(int[] inputArray) {
}
}
Upvotes: 0
Views: 679
Reputation: 1
Here's the fully finished program :D
public static void main(String[] args) {
// Program 1: Average, Min, Max, Trimmed Average of Array
Scanner in = new Scanner(System.in);
int array[] = new int[] { 88, 48, 96, 85, 97, 68, 97 };
int max = getMax(array);
System.out.println("Maximum: " + max);
int min = getMin(array);
System.out.println("Minimum: " + min);
double avg = getAvg(array);
System.out.println("Average: " + avg);
double mean = getTrim(array);
System.out.println("Trimmed mean: " + mean);
}
public static int getMax(int[] inputArray) {
int maxValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] > maxValue) {
maxValue = inputArray[i];
}
}
return maxValue;
}
public static int getMin(int[] inputArray) {
int minValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] < minValue) {
minValue = inputArray[i];
}
}
return minValue;
}
public static double getAvg(int[] inputArray) {
double total = 0;
double avg;
for (int i = 0; i < inputArray.length; i++) {
total = total + inputArray[i];
}
return avg = total / inputArray.length;
}
public static double getTrim(int[] inputArray) {
int max = getMax(inputArray);
int min = getMin(inputArray);
double sum = 0;
for (int i = 0; i < inputArray.length; i++) {
sum = sum + inputArray[i];
if (inputArray[i] > max) {
max = inputArray[i];
}
if (inputArray[i] < min) {
min = inputArray[i];
}
}
return ((double) (sum - max - min)) / (inputArray.length - 2);
}
}
Upvotes: 0
Reputation: 302
Try this:
public static double avgExcludingMinMax(int[] inputArray) {
int max = inputArray[0];
int min = inputArray[0];
long sum = 0;
for(int i = 0; i < inputArray.length; i++){
sum = sum + inputArray[i];
if (inputArray[i] > max) {
max = inputArray[i];
}
if (inputArray[i] < min) {
min = inputArray[i];
}
}
return ((double) (sum - max - min)) / (inputArray.length - 2);
}
Note that this will throw an exception if inputArray.length == 0;
If you want to read more about the statistical background of such an operation, check out https://en.wikipedia.org/wiki/Trimmed_estimator and https://en.wikipedia.org/wiki/Winsorizing!
Upvotes: 1