-5

for now I have to write a java boolean isSymmetric () fucntion that returns true if the invoking matrix is a symmetric matrix; otherwise it returns false. Can anyone help me with the code here or from where to start? Any answer will be appreciated.

Mina Hafzalla
  • 2,681
  • 9
  • 30
  • 44
  • This seems very much like homework, at least try to come up with a solution and you will receive help. Here is some information on symmetry http://en.wikipedia.org/wiki/Symmetric_matrix – Leon Apr 21 '14 at 19:57

1 Answers1

1

You should just Google this stuff. There were plenty of answers out there.

But all you have to do is check if (a,b) is the same as (b,a).

public static boolean isSymetric(int[][] array){
    for(int a = 0; a < array.length; a++){ 
        for(int b = 0; b < array.length; b++){
            if(array[a][b]!=array[b][a]){
                return false;
            }
        }
    }
    return true;
}

In this method the outer most for loop goes through the rows and the inner for loop is going through the columns.

You just have to go through each and every element in the matrix. If array[a][b] == array[b][a] then you can check the next one. If they are not the same then this matrix is not symmetric.

Julius M
  • 76
  • 5
  • You run the risk of index out of bounds exceptions if a matrix is non symmetric – Leon Apr 21 '14 at 20:02
  • @Leon oh you make a really good point here. I just sort of assumed that every matrix will be a square one. Is it even possible to have a symmetric matrix that is not a square matrix ? – Julius M Apr 21 '14 at 20:11
  • Only a square matrix can be symmetric, so a simple if statement for bounds comparison can be used to to confirm that the matrix is indeed square – Leon Apr 21 '14 at 20:15