w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Accept two matrices and check whether they are equal

C# Sharp Array: Exercise-30 with Solution

Write a program in C# Sharp to accept two matrices and check whether they are equal.

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise30 
{  
    public static void Main()
   {
      	int[,] arr1 = new int[50,50];
		int[,] brr1 = new int[50,50];
        int i, j, r1, c1, r2, c2, flag =1;
   
       Console.Write("\n\nAccept two matrices and check whether they are equal :\n ");
       Console.Write("-----------------------------------------------------------\n");   
 
     Console.Write("Input the number of rows in the 1st matrix : ");
	 r1 = Convert.ToInt32(Console.ReadLine());
     Console.Write("Input the number of columns in  the 1st matrix : ");
	 c1 = Convert.ToInt32(Console.ReadLine()); 
   
     Console.Write("Input the number of rows in the 2nd matrix : ");
	 r2 = Convert.ToInt32(Console.ReadLine());
     Console.Write("Input the number of columns in  the 2nd matrix : ");
	 c2 = Convert.ToInt32(Console.ReadLine());  

	 Console.Write("Input elements in the first matrix :\n");
       for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
	           arr1[i,j] = Convert.ToInt32(Console.ReadLine());  
            }
        } 
       Console.Write("Input elements in the second matrix :\n");
       for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
	           brr1[i,j] = Convert.ToInt32(Console.ReadLine()); 
            }
        }   
 	 Console.Write("The first matrix is :\n");
	 for(i=0;i<r1;i++)
	 {
	   for(j=0;j<c1 ;j++)
	     Console.Write("{0}  ",arr1[i,j]);
	    Console.Write("\n");
	 }
	 Console.Write("The second matrix is :\n");
	 for(i=0;i<r2;i++)
	 {
	   for(j=0;j<c2 ;j++)
	     Console.Write("{0}  ",brr1[i,j]);
	    Console.Write("\n");
	 }
	 
   /* Comparing two matrices for equality */

   if(r1 != r2 && c1 != c2)
      {  
         Console.Write("The Matrices Cannot be compared :\n");
      }
     else
      {
    	Console.Write("The Matrices can be compared : \n");
    	for(i=0; i<r1; i++)
    	{
     		for(j=0; j<c2; j++)
     		{
			if(arr1[i,j] != brr1[i,j])
			{
	   			flag = 0;
	   			break;
			}
     		}
   	 }
    if(flag == 1 )
	Console.Write("Two matrices are equal.\n\n");
    else
	Console.Write("But,two matrices are not equal\n\n");
      }
    }	
}

Sample Output:

Accept two matrices and check whether they are equal :                                                        
 -----------------------------------------------------------                                                  
Input the number of rows in the 1st matrix : 2                                                                
Input the number of columns in  the 1st matrix : 2                                                            
Input the number of rows in the 2nd matrix : 2                                                                
Input the number of columns in  the 2nd matrix : 2                                                            
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [1],[0] : 3                                                                                         
element - [1],[1] : 4                                                                                         
Input elements in the second matrix :                                                                         
element - [0],[0] : 5                                                                                         
element - [0],[1] : 6                                                                                         
element - [1],[0] : 7                                                                                         
element - [1],[1] : 8                                                                                         
The first matrix is :                                                                                         
1  2                                                                                                          
3  4                                                                                                          
The second matrix is :                                                                                        
5  6                                                                                                          
7  8                                                                                                          
The Matrices can be compared :                                                                                
But,two matrices are not equal

Flowchart:

Flowchart: Accept two matrices and check whether they are equal

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to accept a matrix and determine whether it is a sparse matrix.
Next: Write a program in C# Sharp to Check whether a Given Matrix is an Identity Matrix.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming