69.8k views
3 votes
Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.centeredAverage({1, 2, 3, 4, 100}) → 3centeredAverage({1, 1, 5, 5, 10, 8, 7}) → 5centeredAverage({-10, -4, -2, -4, -2, 0}) → -3

User Motia
by
4.1k points

1 Answer

0 votes

Answer:

The code to this question can be defined as follows:

public double centeredAverage(ArrayList<Integer> nums) //defining a method centeredAverage that accepts an array nums

{

if ((nums == null) || (nums.size() < 3))//define if block for check simple case value

{

return 0.0;//return float value

}

double sum = 0; //defining a double variable sum

int smallest = nums.get(0);//defining integer variable and assign value

int largest = nums.get(0);//defining integer variable and assign value

for (int i = 0; i < nums.size(); i++) //defining for loop for stor ith value

{

int value = nums.get(i);//defining integer value variable to hold nums value

sum = sum + value;//use sum variable for add value

if (value < smallest)//defining if block to check value less then smallest

smallest = value;//hold value in smallest variable

else if (value > largest)//defining else if block that checks value greater them largest value

largest = value; //hold value in largest variable

}

sum = sum - largest - smallest; // use sum to decrease the sum largest & smallest value

return (sum / (nums.size() - 2)); //use return keyword for return average value

}

Step-by-step explanation:

In the question, data is missing, that's why the full question is defined in the attached file please find it.

In the given code a method "centeredAverage" is used that accepts an array in its parameter and inside the method, if block is used that checks the cash value if it is true, it will return a float value.

In the next step, two integers and one double variable is defined, that use the conditional statement for check hold value in its variable and at the last use return keyword for return average value.

Return the "centered" average of an array of ints, which we'll say is the-example-1
User Aleung
by
4.0k points