C++ Program to Subtract two Matrixes

Write a C++ Program to Subtract two Matrixes with an example. In this C++ matrix subtraction example, we used the C++ nested for loop to iterate matrix from 0 to rows and columns. Within the nested for loop, we subtracted the subarr2 matrix from subarr1 and assigned it to the subtraction matrix. Finally, we print the subtraction matrix items.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the Sub Multi-Dimensional Array rows & Columns =  ";
	cin >> i >> j;
	
	int subarr1[i][j], subarr2[i][j], subtraction[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> subarr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> subarr2[rows][columns];
		}		
	}
	cout << "\nThe Final Result after subtracting subarr2 from subarr1\n ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			subtraction[rows][columns] = subarr1[rows][columns] - subarr2[rows][columns];
			cout << subtraction[rows][columns] << " ";
			//cout << subarr1[rows][columns] - subarr2[rows][columns];
		}
	}

 	return 0;
}
Please Enter the Sub Multi-Dimensional Array rows & Columns =  3 3

Please Enter the First Multi-Dimensional Array Items =  
10 20 30
40 50 60
70 80 90

Please Enter the Second Multi-Dimensional Array Items =  
5 25 12
19 15 32
16 7 8

The Final Result after subtracting subarr2 from subarr1
 5 -5 18 21 35 28 54 73 82

C++ Program to Subtract two Matrixes Example 2

In this C++ matrix subtraction example, we used the following cout statements to display the subtraction at each row and column iteration. Finally, we used one more nested for loop to print the subtraction matrix items.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the Sub Multi-Dimensional Array rows & Columns =  ";
	cin >> i >> j;
	
	int subarr1[i][j], subarr2[i][j], subtraction[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> subarr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> subarr2[rows][columns];
		}		
	}
	for(rows = 0; rows < i; rows++)	{
		cout << "\n---The Subtraction Result of " << rows + 1 << " Row Iteration---\n";
		for(columns = 0; columns < j; columns++) {
			subtraction[rows][columns] = subarr1[rows][columns] - subarr2[rows][columns];
			
			cout << "\nThe Subtraction Result of " << columns + 1 << " Column Iteration = ";
			cout << subarr1[rows][columns] << " - " << subarr2[rows][columns] << " = " << subtraction[rows][columns] << "\n";
		}
	}
	cout << "\nThe Final Result after subtracting subarr2 from subarr1\n ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			cout << subtraction[rows][columns] << "  ";
		}
		cout<<"\n";
	}

 	return 0;
}
C++ Program to Subtract two Matrixes 2