C program to compare two matrices | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, February 18, 2020

C program to compare two matrices | My CS Tutorial

C program to compare two matrices. Comparison of two matrices in c.
Compare matrices | compare two matrices | My CS Tutorial


In this tutorial we will learn  or discuss that how to write a c program code to compare two matrix. In this program we will use arrays.

This c program code will compare two matrixes.

In the first user will enter range of rows and columns of both matrices. After it user enter the elements of first matrix then second matrix.

After it compiler check and exicute the condition and logics and print both matrices and compare it and print the statement that is equal,not equal or matrices can't be compared.


SOURCE CODE


#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,rows_1,col_1,rows_2,col_2,flag=1;
 int a1[50][50],a2[50][50];
 clrscr();
  
 printf("\n ENTER NUMBER(RANGE) OF ROWS AND COLUMNS OF FIRST MATRIX:\n ");
 scanf("%d %d",&rows_1,&col_1);
 printf("\n ENTER NUMBER(RANGE) OF ROWS AND COLUMNS OF SECOND MATRIX:\n ");
 scanf("%d %d",&rows_2,&col_2);

 if(rows_1==rows_2&&col_1==col_2)
 {
 
  //Taking input for first matrix
  
     printf("\n ENTER FIRST MATRIX:\n ");
     for(i=0;i<rows_1;i++)
    {
    for(j=0;j<col_1;j++)
    {
   scanf("%d",&a1[i][j]);
    }
     }
     
     //Taking input for second matrix
     
      printf("\n ENTER SECOND MATRIX:\n ");
      
     for(i=0;i<rows_2;i++)
    {
    for(j=0;j<col_2;j++)
    {
   scanf("%d",&a2[i][j]);
    }
    }

    //Printing given matrices
    
     printf("\n FIRST MATRIX IS:\n ");
     for(i=0;i<rows_1;i++)
    {
    for(j=0;j<col_1;j++)
    {
   printf("%d\t",a1[i][j]);

    }
    printf("\n ");
    }

    printf(" \n SECOND MATRIX IS:\n ");
     for(i=0;i<rows_2;i++)
    {
    for(j=0;j<col_2;j++)
    {
   printf("%d\t",a2[i][j]);
    }
    printf("\n ");
    }

      //Comparing of matrix
      
    for(i=0;i<rows_1;i++)
    {
     for(j=0;j<col_1;j++)
     {
     if(a1[i][j]!=a2[i][j])
     {
        flag=0;
        }
     }
    }

    if(flag==1)
    {
     printf("\n BOTH MATRICES ARE EQUAL.\n ");
    }
    else
    {
     printf("\n BOTH MATRICES ARE NOT EQUAL.\n ");
    }

 }
 else
 {
  printf("\n MATRICES CAN NOT BE COMPARED.\n ");
 }

getch();
}


OUTPUT:





Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment