Checking for a Symmetric Matrix

Write a program to declare a square matrix A[][] of order MxM where
‘M’ is the number of rows and the number of coloumns, such that M must
be greater than 2 and less than 10. Accept the value of M as user
input. Display an appropriate message for invalid input. Allow the user
to input integers into this matrix. Perform the following tasks:

(a) Display the original matrix.

(b) Check if the given matrix is Symmetric or not. A square matrix is
said to be Symmetric, if the element of the ith row and the jth
column is equal to the element of jth row and the ith
column.

(c) Find the sum of the elements of left diagonal and the sum of the
elements if right diagonal of the matrix and display them.

Test your program for the following data and some random data.

Example 1:

INPUT: M = 3
 1 2 3
 2 4 5
 3 5 6
OUTPUT:
ORIGINAL MATRIX
 1 2 3
 2 4 5
 3 5 6

THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10

Example 2

INPUT: M = 4
 7 8 9 2
 4 5 6 3
 8 5 3 1
 7 6 4 2
OUTPUT:
ORIGINAL MATRIX
 7 8 9 2
 4 5 6 3
 8 5 3 1
 7 6 4 2

THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20

Example 3

INPUT: M = 12
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE

The Java implementation for “Checking for a Symmetric Matrix” is as
follows:

import java.util.*;
class SymmetricMatrix{
    public static void display( int A[][] ){
        for( int row=0; row < A.length; row++ ){
            System.out.print( "\t\t\t" );
            for( int col=0; col < A.length; col++ ){
                System.out.print( A[row][col]+"\t" );
            }
            System.out.println();
        }
    }

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

    public static void computeAndDisplaySumOfDiagonals( int A[][] ){
        int sumOfLeftDiagonal=0, sumOfRightDiagonal=0;
        for( int i=0; i < A.length; i++ ){
            sumOfLeftDiagonal += A[i][i];
            sumOfRightDiagonal += A[i][A.length -i -1];
        }
        System.out.println( "The sum of the left diagonal = " + sumOfLeftDiagonal );
        System.out.println( "The sum of the right diagonal = " + sumOfRightDiagonal );
    }

    public static void main( String args[] ){
        int M;
        Scanner sc = new Scanner( System.in );
        System.out.print( "M = " );
        M = sc.nextInt();
        if(M <= 2 || M >= 10){
            System.out.println( "OUTPUT:\t\tTHE MATRIX SIZE IS OUT OF RANGE" );
        }else{
            int A[][] = new int[M][M];
            for( int row=0; row < M; row++ ){
                for( int col=0; col < M; col++ ){
                    A[row][col] = sc.nextInt();
                }
            }
            System.out.println( "ORIGINAL MATRIX" );
            display(A);
            if( isSymmetric(A) ) System.out.println( "THE GIVEN MATRIX IS SYMMETRIC" );
            else System.out.println( "THE GIVEN MATRIX IS NOT SYMMETRIC" );
            computeAndDisplaySumOfDiagonals(A);
        }
    }
}

In the above program on “Checking for a Symmetric Matrix” was asked in the ISC Computer Practical Examination 2014. The first point to be noted in the above Java program is the function for finding the sum of left diagonal and the sum of right diagonal. The function computeAndDisplaySumOfDiagonals() is having only one loop which is iterating for A.length times.
For finding the sum of left diagonal we are increasing the index of both row and column at the same time, i.e. first we will add A[0][0], then A[1][1] and so on. For finding the sum of right diagonal we are increasing the index of row because starting from the top right of the matrix, each successive diagonal element is on the next row. the coloumn index is actually decreasing from A.length to 0. It’s decreasing because the value of i in the expression A.length -i -1 is increasing. The value of A.length and 1 is obviously constant. The value 1 is being subtracted because arrays in Java always start fom 0 and not doing so will result in
ArrayIndexOutOfBounds exception. The computational complexity of the function in Big O notation is therefore O(N).

The loop in the function isSymmetric() also deserves some attention. The outer loop is running for the entire range of rows in the double. dimension array because we want to consider all rows. But, the inner loop is running till the number of current rows in each iteration; meaning we will only check for the lower triangular part of the square array and compare it with the upper triangular part of the square array. This way we will be checking each pair only once. For example if we have checked the pair A[2][3] and A[3][2], there is no need to check for A[3][2] and A[2][3]. This will result in increased efficiency.

Follow me

Vinay Singh

Computer Teacher at Saint John's Academy
I am a GNU/Linux enthusiast and a computer science teacher by profession based at Allahabad, India. My hobbies include reading (mostly computer related stuff) and photography.
Follow me

Latest posts by Vinay Singh (see all)

Leave a Reply