Saturday, August 24, 2019

Program to Check Equality of Two Matrixes


This is a BlueJ program on two matrixes to check whether they are equal or not.

import java.util.*;
class Search
{
    int i,j,n,m;
    int arr1[][], arr2[][];
    Scanner sc=new Scanner(System.in);
public void take()
{    
    System.out.print("\nEnter Row Numbers: ");
     n=sc.nextInt();
     System.out.print("\nEnter Column Numbers: ");
     m=sc.nextInt();
     arr1=new int[n][m];
     arr2=new int [n][m];
     for(i=0;i< n;i++)
     {
        for(j=0;j< m;j++)
        {
          System.out.println("\nFor 1st Matrix ");
          System.out.print("\nValue: ");
          arr1[i][j]=sc.nextInt();
          System.out.println("\nFor 2nd Matrix ");
          System.out.print("\nValue: ");
          arr2[i][j]=sc.nextInt();
     }
 }
}
    public void show()
    {
        int c=0;
        System.out.println("\n1st Matrix");
        for(i=0;i< n;i++)
        {
          for(j=0;j< m;j++)
          {
            System.out.print(" "+arr1[i][j]);
          }
          System.out.println();
        }
        System.out.println("\n2nd Matrix");
        for(i=0;i< n;i++)
        {
          for(j=0;j< m;j++)
          {
            System.out.print(" "+arr2[i][j]);
          }
          System.out.println();
        }
       
            for(i=0;i< n;i++)
        {
          for(j=0;j< m;j++)
          {
              if(arr1[i][j]!=arr2[i][j])
              {
                   c=1;
                   break;
                }
            }
            if(c==1)
            break; // this breaks the outer loop
        }
        if(c==1)
        System.out.println("\nNot Equal Matrixes.");
        else
        System.out.println("\nEqual Matrixes.");
    }
      
    public static void main(String args[])
    {
        Search ob=new Search();
        ob.take();
        ob.show();
   }
 }


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner