Question 6-3

[15 pts] A matrix is symmetric if, for each i and j, ai,j = aj,i. The following is an example of a symmetric matrix.

02345
231036
463620
Another way of defining it: A symmetric matrix can be reflected across its main diagonal (top left corner to bottom right corner) to obtain the same matrix.

Complete the following method so that it returns true only if the two-dimensional array parameter mat is symmetric. Your solution may assume that the matrix is square (as many columns as rows).

public static boolean isSymmetric(int[][] mat) {



}
(Your solution shouldn't really be this long!)

Solution

public static boolean isSymmetric(int[][] mat) {
    for(int i = 0; i < mat.length; i++) {
        for(int j = 0; j < mat.length; j++) {
            if(mat[i][j] != mat[j][i]) return false;
        }
    }
    return true;
}

Back to 8:00 Quiz 6