-2
 public static int[] getMinimumSymmetricOfEveryRow (int [][] A)
{
    int min = Integer.MAX_VALUE;
    int[] arr=new int[A.length];
    for (int i = 0; i < A.length; i++){
        for (int j = 0; j < A[i].length; j++){
            if (A[i][j] < min && isSymmetric(A[i][j])){
                arr[i] = A[i][j];
                }
        }
    }
    return arr;
}

This is my function which return array of min symmetric element from each row in 2D array. It works fine for array[n][n] and but not for array[m][n]... ( it return m elements instead of n). I don't know what is the problem ??

  • If you step through the program, either mentally or with a debugger I think you'll quickly realise why it returns m elements. And if m is the number of rows I'd expect a method called getMinimumSymmetricOfEveryRow to return m results. Also, do you not need to reset min for each row? (Put it inside the outer loop) – Roger Lindsjö Mar 01 '13 at 09:55
  • oh, thank, about reset i forgot..( Can you tell me how go though the loop in columns not rows and inner loop - elements of colum in some row? – Roman Chyzh Mar 01 '13 at 10:05

1 Answers1

0

Assuming the input array is of the format[row][column] and the method should be getMinimumSymmetricOfEveryColumn the you could write your method as:

 public static int[] getMinimumSymmetricOfEveryColumn(int[][] matrix) {
    int[] columnMinimum = new int[matrix[0].length];
    for (int colimnIndex = 0; colimnIndex < matrix[0].length; colimnIndex++){
        int currentMinimum = Integer.MAX_VALUE;
        for (int rowIndex = 0; rowIndex < matrix.length; rowIndex++){
            int currentElement = matrix[rowIndex][colimnIndex];
            if (currentElement < currentMinimum && isSymmetric(currentElement)){
                columnMinimum[colimnIndex] = currentElement;
            }
        }
    }
    return columnMinimum;
}
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53