C# Program to Check the Equality of Matrices

«
»

This is a C# Program to check the equality of matrices.

Problem Description

This C# Program Checks the Equality of Matrices.

Problem Solution

The dimensions of the two matrices are obtained from the user and its equality is checked.

advertisement
Program/Source Code

Here is source code of the C# Program to Check the Equality of Matrices. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Check the Equality of Matrices
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
    class Program
    {
        int x;
        public static void Main(string[] args)
        {
            int m, n, i, j;
            Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");
            m = Convert.ToInt16(Console.ReadLine());
            n = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[10, 10];
            Console.Write("\nEnter The First Matrix : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
            int[,] B = new int[10, 10];
            Console.Write("\nEnter The Second Matrix:");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    B[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
            Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.WriteLine("\nMatrix B: ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(B[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            int[,] C = new int[10, 10];
            bool flag = true;
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    if (A[i, j] == B[i, j])
                    {
                        flag=true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
            }
            if (flag)
            {
                Console.WriteLine("Matrices are Equal  ");
            }
            else
            {
                Console.WriteLine("Not Equal ");
            }            
            Console.Read();
        }
    }
}
Program Explanation

In this C# program, we are reading the dimension of the two matrices the number of rows and columns using ‘m’ and ‘n’ variables respectively. Using for loop enter the coefficient element values for first and second matrices respectively.

advertisement

Using for loop, check the equality of matrices. If else condition statement is used to check that the value of A[i, j] variable is equal to value of B[i, j] variable. If the condition is true, then execute the statement and assign the value of ‘flag’ variable as true.

Otherwise, if the condition is false then execute the else statement and assign the value of ‘flag’ variable as false. Using if else condition statement print the equality of matrices.

Runtime Test Cases
 
Enter Number Of Rows And Columns Of Matrices A and B : 3  3
Enter the First Matrix : 
1 2 3
2 3 4
3 4 5
Enter the Second Matrix :
1 2 3
2 1 4
1 1 5
Matrix A :
1 2 3
2 3 4
3 4 5
Matrix B :
1 2 3
2 1 4
1 1 5
Not Equal

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn