C# Program to Check equality of two matrices

Check The Equality of Two Matrices

C# program to check the equality of two matrices has been shown here. Two matrices $[A]_{m \times n}$ and $[B]_{p \times q}$ are considered to be equal if both of the following conditions are satisfied

(i) Number of rows and columns are same for both of the matrices i.e. $m = p$ and $n = q$

(ii) Each elements of $A$ is equal to corresponding element of $B$ i.e. $A_{ij} = B_{xy}$ for each $i \in m$, $j \in n$, $x \in p$, $y \in q$, $i = x$ and $j = y$.






1. C# Program to check the equality of two matrices

Code has been copied
/*********************************************
           alphabetacoder.com
 C# program to check equality of two matrices
**********************************************/

using System;

namespace MatrixEquality {
    class Program {
        static void Main(string[] args) {
            //declare and initialize variable
            int m, n, p, q, flag = 0, i, j;
            int[, ] A = new int[10, 10];
            int[, ] B = new int[10, 10];

            //take input of the order of first matrix
            Console.WriteLine("Enter the number of row and column of first matrix=");
            m = Convert.ToInt32(Console.ReadLine());
            n = Convert.ToInt32(Console.ReadLine());

            //take input of the first matrix
            Console.WriteLine("Enter the first matrix of order " + m + " x " + n + " = ");
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    A[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            //take input of the order of second matrix
            Console.WriteLine("Enter the number of row and column of second matrix=");
            p = Convert.ToInt32(Console.ReadLine());
            q = Convert.ToInt32(Console.ReadLine());

            //take input of the second matrix
            Console.WriteLine("Enter the first matrix of order " + p + " x " + q + " = ");
            for (i = 0; i < p; i++) {
                for (j = 0; j < q; j++) {
                    B[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            // check if order of matrices are same
            // if not same order then check each corresponding elements
            if (m != p || n != q) {
                Console.WriteLine("\nMatrices are of different order,hence not equal");
                flag = 1;
            } else {
                //check equality of each corresponding elements
                for (i = 0; i < p; i++) {
                    for (j = 0; j < q; j++) {
                        if (A[i, j] != B[i, j]) {
                            // inequality spotted 
                            Console.WriteLine("\nMatrices are not equal. Element mismatch at " + i + 1 + " row " + j + 1 + " column");
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1)
                        break;
                }
            }
            if (flag == 0)
                Console.WriteLine("\nMatrices are equal");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the number of row and column of first matrix=

2

2

Enter the first matrix of order 2 x 2 =

1

2

3

4

Enter the number of row and column of second matrix=

2

3

Enter the first matrix of order 2 x 3 =

1

2

3

4

5

6


Matrices are of different order,hence not equal