Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PTRACTCALS ~ 2014 Symmetric Matrix

Friday, August 1, 2014

ISC COMPUTER SCIENCE PTRACTCALS ~ 2014 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 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:
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 in the i th row and j th column is equal to the element of the j th row and i th
column.
c)
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 for the following data and some random data:
Example 1INPUT:
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
*/
    /* ISC COMPUTER SCIENCE PRACTICALS SOLVED 2014 ~ Symmetric Matrix */
import java.util.*;
public class SymmetricMatrix {
    public static void main(String[] args) {
        int M;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF M:");
        M = in.nextInt();
        while( M > 10 || M < 2) {
            System.out.println("OUT OF RANGE, INPUT AGAIN:");
            M = in.nextInt();
            }
        Matrix m = new Matrix(M);   
        m.inputMatrix();
        System.out.println("OUTPUT:");
        m.displayMatrix();
        m.diagonalSum();
        in.close();
        }
    }   
class Matrix {
    Matrix(int M) {
        odr = M;
        Mat = new int[M][M];
        leftDsum = 0;
        rightDsum = 0;
        }
    public void inputMatrix() {
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < odr; i++) {
            for(int j = 0; j < odr; j++) {
                System.out.println("Input the element:");
                Mat[i][j] = in.nextInt();
                }
            }
        }   
    public void displayMatrix() {
        flag = true;
        System.out.println("ORIGINAL MATRIX:");
        for(int i = 0; i < odr; i++) {
            for(int j = 0; j < odr; j++) {
                if( Mat[i][j] != Mat[j][i])
                    flag = false;
                if(j == i)
                    leftDsum += Mat[i][j];
                if( i+j == odr-1)
                    rightDsum += Mat[i][j];       
                System.out.print(Mat[i][j]+"  ");
                }
            System.out.println();
            }
        if( flag == true )
            System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
        else
            System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");   
        }   
    public void diagonalSum() {
        System.out.println();
        System.out.println("The sum of the left diagonal: "+leftDsum);
        System.out.println();
        System.out.println("The sum of the right diagonal: "+rightDsum);
        System.out.println();   
        }
    private int odr;   
    private int[][] Mat;
    private boolean flag;
    private int leftDsum;
    private int rightDsum;
    }   

No comments:

Post a Comment