Symmetric Matrix ISC Computer Science 2014 Practical

Write a program to declare a square matrix a[][] of order (m × m) where 'm' is the number of rows and the number of columns 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 an invalid input. Allow the user to input integers into this matrix. Perform the following tasks:

  1. Display the original matrix.
  2. 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 jth column is equal to the element of the jth row and ith column.
  3. Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the matrix and display them.
Test your program with the sample 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 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 = 22
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE.

import java.util.Scanner;
class Symmetric{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("m = ");
        int m = Integer.parseInt(in.nextLine());
        if(m < 3 || m > 9){
            System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
            return;
        }
        int a[][] = new int[m][m];
        int left = 0;
        int right = 0;
        System.out.println("Enter matrix elements:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                a[i][j] = Integer.parseInt(in.nextLine());
                if(i == j)
                    left += a[i][j];
                if(i + j == m - 1)
                    right += a[i][j];
            }
        }
        boolean isSymmetric = true;
        outer:
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                if(a[i][j] != a[j][i]){
                    isSymmetric = false;
                    break outer;
                }
            }
        }
        System.out.println("ORIGINAL MATRIX:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
        if(isSymmetric)
            System.out.println("THE GIVEN MATRIX IS SYMMETRIC.");
        else
            System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC.");
        System.out.println("The sum of the left diagonal = " + left);
        System.out.println("The sum of the right diagonal = " + right);
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory