Question

In: Computer Science

This is JAVA Return the centered average of an array of ints, which we'll say is...

This is JAVA

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 has a length of 3 or more.

1

public int centeredAverage(int[] nums)

2

{

3

    

4

}

Solutions

Expert Solution

public int centeredAverage(int[] nums)

{

int min,max,sum=0;

min=nums[0];

max=nums[0];

for(int i=0;i<nums.length;i++)

{

if(max<nums[i])

max=nums[i];

if(min>nums[i])

min=nums[i];

sum+=nums[i];

}

int avg=(sum-max-min)/(nums.length-2);

return avg;

  

}

Complete program:-

import java.util.Scanner;

public class MyClass {

  

public static int centeredAverage(int[] nums)

{

int min,max,sum=0;

min=nums[0];

max=nums[0];

for(int i=0;i<nums.length;i++)

{

if(max<nums[i])

max=nums[i];

if(min>nums[i])

min=nums[i];

sum+=nums[i];

}

int avg=(sum-max-min)/(nums.length-2);

return avg;

  

}

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the array size : ");

int size=sc.nextInt();

int []arr=new int[size];

System.out.println("Enter the array elements : ");

for(int i=0;i<size;i++)

arr[i]=sc.nextInt();

int avg=centeredAverage(arr);

System.out.println("Average : "+avg);

}

}


Related Solutions

Java Language The array s of ints contain integers each of which is between 1 and...
Java Language The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array { 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 } then your code should set ordinals to the array { "1st", "2nd", "3rd",...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
The array s of ints contain integers each of which is between 1 and 1000 (inclusive)....
The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array { 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 } then your code should set ordinals to the array { "1st", "2nd", "3rd", "4th", "5th",...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Given an array, return the element at which the "pattern" breaks. For example, in the array...
Given an array, return the element at which the "pattern" breaks. For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3. PLEASE USE DIVIDE AND CONQUER. Thank you. Python please or just a description of the algorithm
a) Write C code initialize an array of ints to the last four digits of your...
a) Write C code initialize an array of ints to the last four digits of your phone number. Use a loop to add the digits. Calculate and display the average of the digits. b) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
Given a string and a non-negative int n, we'll say that the front of the string...
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front; frontTimes("Chocolate", 2) → "ChoCho" frontTimes("Chocolate", 3) → "ChoChoCho" frontTimes("Abc", 3) → "AbcAbcAbc" Must work the following problem using a while loop or do while.
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT