1

having some issues with comparing my mean element of my 2d array to the closest element value to the mean. The main issue is i'm not sure how to proceed beyond using math.abs to compare the array elements to the mean.

My code.

public class exercise_2{
public static int[] closestToMean (double[][] array)
{
    int sum = 0;
    for (int i=0; i < array.length; i++)
{
        for (int j=0; j < array.length; j++)
    {
        sum += array[i][j];
    }
}
    double mean = (double) sum / array.length;
    System.out.println("Mean = " + mean);
    //calculate mean of array elements i + j


    //closest to mean
    int distance = Math.abs(array[0] - mean);
    int i = 0;
    for (int c = 1; c < array.length; c++)
    {
        int cdistance = Math.abs(array[c] - mean);
        if (cdistance < distance)
        {
            i = c;
            distance = cdistance;
        }
    }
    double mean = array[i];
    System.out.println("Closest array element = " + mean);
    //print closest to mean array element
}

public static void testClosestToMean()
{
    exercise_2 ex2 = null;
    ex2.closestToMean();
    //invoke method closestToMean()
}

public static void main()
{
    exercise_2 ex2 = null;
    ex2.testClosestToMean();
    //invoke testClosestToMean()
}

}

7
  • You mean, Math.abs produces wrong results? Or there is a condition to write a program without using Math library? Commented Apr 5, 2017 at 11:18
  • It would be best to define sum as double, not int, as the array itself is double[][]. But your test for the closest element seems to be fine. You're just defining mean twice, which will result in a compilation error. Commented Apr 5, 2017 at 11:20
  • (array[0] - mean); says i cant use binary operator - as its a bad operator. I can use maths.abs but just dont know what operators to pass to it to allow it to work. Commented Apr 5, 2017 at 11:21
  • 1
    Remember that your array is two dimensional. Commented Apr 5, 2017 at 11:22
  • also make distance a double, otherwise non integer test data could give incorrect results Commented Apr 5, 2017 at 11:25

1 Answer 1

1

The following code should do what you want:

@Test
public void test() {
  double[][] numbers = new double[][] {new double[] {1, 3, 5, 8}, new double[] {1, 2}};
  int[] indices = getClosestToMeanIndices(numbers);
  System.out.println("Closest element indices: " + indices[0] + ", " + indices[1]);
  System.out.println("Closest element: " + numbers[indices[0]][indices[1]]);
}

private int[] getClosestToMeanIndices(double[][] numbers) {
  if (numbers.length == 0) {
    throw new IllegalArgumentException("Input is empty");
  }

  // Calculate the mean
  double sum = 0;
  double elementCount = 0;
  for (double[] number : numbers) {
    for (double n : number) {
      sum += n;
      elementCount++;
    }
  }
  double mean = sum / elementCount;

  // Find closest index
  int[] indices = new int[] {-1, -1};

  double distance = Math.abs(mean - numbers[0][0]);
  for (int i = 0; i < numbers.length; i++) {
    for (int j = 0; j < numbers[i].length; j++) {
      double currentDistance = Math.abs(mean - numbers[i][j]);
      if (currentDistance < distance) {
        distance = currentDistance;
        indices[0] = i;
        indices[1] = j;
      }
    }
  }

  return indices;
}

Output:

Closest element indices: 0, 1
Closest element: 3.0


What you forgot in your code is that for the mean you do not only need to use the length of your array, but the full number of elements inside the 2D array.

In the end you can then check the distances and return the according indices.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.