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