C++ Program to Check two Matrixes are Equal

Write a C++ Program to Check two Matrixes are Equal with an example. In this Matrix equal example, we used nested for loops to iterate the items. Next, we used the If statement (if(a[rows][columns] != b[rows][columns])) within the for loop to check whether each item in a matrix not equal to the b matrix item.

If True, change the isEqual to 0 and apply the break statement to exit the loop. Next, we used the isEqual variable within the If Else statement to print the matrix output.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, isEqual;
	
	cout << "\nPlease Enter the rows and Columns a Equal Matrix =  ";
	cin >> i >> j;
	
	int a[i][j], b[i][j];
	
	cout << "\nPlease Enter the First Matrix Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> a[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Matrix Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> b[rows][columns];
		}		
	}
	
	isEqual = 1; 	
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		if(a[rows][columns] != b[rows][columns])
			{
			  	isEqual = 0;
			  	break;
			}    
   	 	}
  	}
 	if(isEqual == 1)
 	{
 		cout << "\nMatrix a is Equal to Matrix b";		
	}
	else
	{
		cout << "\nMatrix a is Not Equal to Matrix b";
	}	

 	return 0;
}
C++ Program to Check two Matrixes are Equal 1

Let me try two different inputs.

Please Enter the rows and Columns a Equal Matrix =  2 2

Please Enter the First Matrix Items =  
1 2
3 4

Please Enter the Second Matrix Items =  
1 2
3 5

Matrix a is Not Equal to Matrix b

In this Matrix Equal code, we used a few extra cout statements to display the iteration numbers, row, column number, and items. Please refer to C++ programs.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, isEqual;
	
	cout << "\nPlease Enter the rows and Columns =  ";
	cin >> i >> j;
	
	int a[i][j], b[i][j];
	
	cout << "\nPlease Enter the First Mat Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> a[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Mat Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> b[rows][columns];
		}		
	}
	
	isEqual = 1; 	
 	for(rows = 0; rows < i; rows++)
  	{
  		cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
   		for(columns = 0; columns < j; columns++)
    	{
    		cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " << 
			  	columns << ", and Row Number = " << rows;
			cout << "\nThe Result of a[" << rows << "][" << columns << "] != " 
				<< "b[" << rows << "][" << columns << "] = "  << (a[rows][columns] != b[rows][columns]);
      		if(a[rows][columns] != b[rows][columns])
			{
			  	isEqual = 0;
			  	break;
			}    
   	 	}
  	}
 	if(isEqual == 1)
 	{
 		cout << "\nMatrix a is Equal to b";		
	}
	else
	{
		cout << "\nMatrix a is Not Equal to b";
	}	

 	return 0;
}
Please Enter the rows and Columns =  2 2

Please Enter the First Mat Items =  
1 2
3 4

Please Enter the Second Mat Items =  
1 2
3 4

Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
The Result of a[0][0] != b[0][0] = 0
Column Iteration = 2, Column Number = 1, and Row Number = 0
The Result of a[0][1] != b[0][1] = 0
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
The Result of a[1][0] != b[1][0] = 0
Column Iteration = 2, Column Number = 1, and Row Number = 1
The Result of a[1][1] != b[1][1] = 0
Matrix a is Equal to b

C++ Program to Check two Matrixes are Equal using While loop

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, isEqual;
	
	cout << "\nPlease Enter the rows and Columns =  ";
	cin >> i >> j;
	
	int a[i][j], b[i][j];
	
	cout << "\nPlease Enter the First Matrix Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> a[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Matrix Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> b[rows][columns];
		}		
	}
	
	isEqual = 1; 
	rows = 0; 	
 	while(rows < i)
  	{
  		columns = 0;
   		while(columns < j)
    	{
      		if(a[rows][columns] != b[rows][columns])
			{
			  	isEqual = 0;
			  	break;
			}
			columns++;
   	 	}
   	 	rows++;
  	}
  	
 	(isEqual == 1) ? cout << "\nMatrix a is Equal to b" : 
	 	cout << "\nMatrix a is Not Equal to b";	

 	return 0;
}
Please Enter the rows and Columns =  3 3

Please Enter the First Matrix Items =  10 20 30 40 50 60 70 80 90

Please Enter the Second Matrix Items =  10 20 30 40 50 60 70 80 90

Matrix a is Equal to b