Wednesday 9 October 2013

Program to chk whether a matrix is symmetric or not

//Program to chk whether a matrix is symmetric or not
//Program by Ahlam Ansari
import java.util.*;
class Symmetry
{
    public static void main(String [] ahlam)
    {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the no of Rows");
        int row=scan.nextInt();
        System.out.println("Enter the no of Columns");
        int col=scan.nextInt();
        int arr[][]=new int [row][col];
        System.out.println("Enter the values");
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("enter the values of row "+ i);
            for(int j=0;j<arr[i].length;j++)
            {
                arr[i][j]=scan.nextInt();
            }
        }
        //transpose of arr
        int [][] arrtrans=new int [col][row];
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("enter the values of row "+ row);
            for(int j=0;j<arr[i].length;j++)
            {
                arrtrans[j][i]=arr[i][j];
            }
        }
        /*display*/
        System.out.println("the orignal matrix is :");
        for(int i=0;i<arr.length;i++)
        {
            for(int j=0;j<arr[i].length;j++)
            {
                System.out.print(" "+arr[i][j]);
            }
        System.out.println();
        }


        System.out.println("the transpose is :");
        for(int i=0;i<arrtrans.length;i++)
        {
            for(int j=0;j<arrtrans[i].length;j++)
            {
                System.out.print(" "+arrtrans[i][j]);
            }
        System.out.println();
        }
       
        //check symmetry
        boolean flag=true;
        outer:for(int i=0;i<arrtrans.length;i++)
        {
            for(int j=0;j<arrtrans[i].length;j++)
            {
                if(arr[i][j]!=arrtrans[i][j])
                {    flag=false;
                    break outer;
                }
            }
        }
        if (flag==false)
        {
            System.out.println("Not Symmetric");
        }
        else
        {
            System.out.println("Symmetric");
        }
    }
}   
   
/*
C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>javac Symmetry.java

C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>java Symmetry
Enter the no of Rows
3
Enter the no of Columns
3
Enter the values
enter the values of row 0
1
2
3
enter the values of row 1
1
2
3
enter the values of row 2
1
2
3
enter the values of row 3
enter the values of row 3
enter the values of row 3
the orignal matrix is :
 1 2 3
 1 2 3
 1 2 3
the transpose is :
 1 1 1
 2 2 2
 3 3 3
Not Symmetric

C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>
 */

No comments:

Post a Comment