Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top