C program to check whether two matrices are equal or not

Write a C program to enter elements in two matrices and check whether both matrices are equal or not. C program to check whether elements of two matrices are equal or not. Logic to check if two matrices are equal or not in C programming.

Required knowledge

Basic C programming, C for loop, ArrayS

Equality of matrix

Two matrices are said to be equal if and only if they are of same size and they have equal corresponding entries. Equality of two matrices A and B can be defined as -

Aij = Bij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n).

Equal matrices

Both the matrices are of same dimension and also their corresponding elements are equal. Hence both Matrix A and Matrix B are equal.

Program to check matrix equality




 
/**
 * C program to check whether two matrices are equal or not
 */

#include <stdio.h<

#define SIZE 3 // Matrix size

int main()
{
    int A[SIZE][SIZE]; 
    int B[SIZE][SIZE];

    int row, col, isEqual;

    /* Input elements in first matrix from user */
    printf("Enter elements in matrix A of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix from user */
    printf("\nEnter elements in matrix B of size %dx%d: \n",SIZE,SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /* Assumes that the matrices are equal */
    isEqual = 1;

    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /*
             * If the corresponding entries of matrices are not equal
             */
            if(A[row][col] != B[row][col])
            {
                isEqual = 0;
                break;
            }
        }
    }

    /*
     * Checks the value of isEqual
     * As per our assumption if isEqual contains 1 means both are equal
     * If it contains 0 means both are not equal
     */
    if(isEqual == 1)
    {
        printf("\nMatrix A is equal to Matrix B");
    }
    else
    {
        printf("\nMatrix A is not equal to Matrix B");
    }

    return 0;
}



Output:

Enter elements in matrix A of size 3x3:
9 8 7
6 5 4
3 2 1
Enter elements in matrix B of size 3x3:
9 8 7
6 5 4
3 2 1
Matrix A is equal to Matrix B
Process returned 0 (0x0) execution time : 37.392 s
Enter elements in matrix A of size 3x3:
10 20 30
40 50 60
70 80 90
Enter elements in matrix B of size 3x3:
10 20 50
40 50 60
70 80 90
Matrix A is not equal to Matrix B
Process returned 0 (0x0) execution time : 53.801 s

Program to check matrix equality




/**
 * C program to check whether two matrices are equal or not
 */

#include 

int main()
{
    int A[10][10];
    int B[10][10];

    int row, col, isEqual,m,n;
    printf("Enter number of rows:");
    scanf("%d",&m);
    printf("Enter of columns:");
    scanf("%d",&n);

    /* Input elements in first matrix from user */
    printf("Enter elements in matrix A of size %dx%d: \n", m, n);
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix from user */
    printf("\nEnter elements in matrix B of size %dx%d: \n",m,n);
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /* Assumes that the matrices are equal */
    isEqual = 1;

    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            /*
             * If the corresponding entries of matrices are not equal
             */
            if(A[row][col] != B[row][col])
            {
                isEqual = 0;
                break;
            }
        }
    }

    /*
     * Checks the value of isEqual
     * As per our assumption if isEqual contains 1 means both are equal
     * If it contains 0 means both are not equal
     */
    if(isEqual == 1)
    {
        printf("\nMatrix A is equal to Matrix B");
    }
    else
    {
        printf("\nMatrix A is not equal to Matrix B");
    }

    return 0;
}



Output:

Enter number of rows:2
Enter of columns:2
Enter elements in matrix A of size 2x2:
1 2
3 4
Enter elements in matrix B of size 2x2:
1 2
3 4
Matrix A is equal to Matrix B
Process returned 0 (0x0) execution time : 12.565 s
Enter number of rows:2
Enter of columns:2
Enter elements in matrix A of size 2x2:
1 2
3 5
Enter elements in matrix B of size 2x2:
1 2
3 4
Matrix A is not equal to Matrix B
Process returned 0 (0x0) execution time : 17.766 s



Instagram