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()
}

}

blockoblock
  • 53
  • 10
  • You mean, Math.abs produces wrong results? Or there is a condition to write a program without using Math library? – Gyrotank Apr 05 '17 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. – RealSkeptic Apr 05 '17 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. – blockoblock Apr 05 '17 at 11:21
  • 1
    Remember that your array is **two dimensional**. – RealSkeptic Apr 05 '17 at 11:22
  • also make distance a double, otherwise non integer test data could give incorrect results – Vorsprung Apr 05 '17 at 11:25
  • so create another double distance = Math.abs(array[0] - mean); (with different name) and another for loop iterating over the values? – blockoblock Apr 05 '17 at 11:30
  • nvm im an idiot. Added another dimension for my array and changed values to double and seems to be working now. Cheers. – blockoblock Apr 05 '17 at 11:49

1 Answers1

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.

JDC
  • 4,247
  • 5
  • 31
  • 74