C++ Program to Perform Arithmetic Operations on Matrix

Write a C++ Program to Perform Arithmetic Operations on Matrix with an example. In this C++ matrix arithmetic operations example, we allow users to enter the matrix sizes and matrixes items. Next, we used the C++ nested for loop to iterate matrix from 0 to rows and columns. Within the nested for loop, we performed arithmetic operations such as addition, division, subtraction, multiplication, and modules on both the matrixes and assigned them to new matrixes. Finally, we used one more nested for loop to print the matrix items.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the rows and Columns of a Multi-Dimensional Array =  ";
	cin >> i >> j;
	
	int arr1[i][j], arr2[i][j], add[i][j], sub[i][j], mul[i][j], mod[i][j];
	float div[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr2[rows][columns];
		}		
	}
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			add[rows][columns] = arr1[rows][columns] + arr2[rows][columns];
			sub[rows][columns] = arr1[rows][columns] - arr2[rows][columns];
			mul[rows][columns] = arr1[rows][columns] * arr2[rows][columns];
			mod[rows][columns] = arr1[rows][columns] / arr2[rows][columns];
		}
	}
	cout << "\nAdd\t Sub\t Mul\t Div\tMod \n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			cout << add[rows][columns] << "\t";
			cout << sub[rows][columns] << "\t";
			cout << mul[rows][columns] << "\t";
			cout << mod[rows][columns] << "\t";
			cout << div[rows][columns] << "\t\n";
		}
	}

 	return 0;
}
C++ Program to Perform Arithmetic Operations on Matrix 1

C++ Program to Perform Arithmetic Operations on Matrix Example 2

This C++ Matrix arithmetic operations program is the same as above. Here, we used the cout statement to show the result of nested for loop in a row and column iteration-wise. 

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the rows and Columns of a Multi-Dimensional Array =  ";
	cin >> i >> j;
	
	int arr1[i][j], arr2[i][j], add[i][j], sub[i][j], mul[i][j], mod[i][j];
	float div[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr2[rows][columns];
		}		
	}
	for(rows = 0; rows < i; rows++)	{
		cout << "\nThe Result of the " << rows + 1 << " Row Iteration\n";
		for(columns = 0; columns < j; columns++) {
			add[rows][columns] = arr1[rows][columns] + arr2[rows][columns];
			sub[rows][columns] = arr1[rows][columns] - arr2[rows][columns];
			mul[rows][columns] = arr1[rows][columns] * arr2[rows][columns];
			mod[rows][columns] = arr1[rows][columns] / arr2[rows][columns];
			div[rows][columns] = arr1[rows][columns] % arr2[rows][columns];
			
			cout << "\nThe Result of the " << columns + 1 << " Column Iteration\n";
			cout << arr1[rows][columns] << " + " << arr2[rows][columns] << " = " << add[rows][columns] << "\n";
			cout << arr1[rows][columns] << " - " << arr2[rows][columns] << " = " << sub[rows][columns] << "\n";
			cout << arr1[rows][columns] << " * " << arr2[rows][columns] << " = " << mul[rows][columns] << "\n";
			cout << arr1[rows][columns] << " / " << arr2[rows][columns] << " = " << mod[rows][columns] << "\n";
			cout << arr1[rows][columns] << " % " << arr2[rows][columns] << " = " << div[rows][columns] << "\n";
		}
	}
	cout << "\nAdd\t Sub\t Mul\t Div\tMod \n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			cout << add[rows][columns] << "\t";
			cout << sub[rows][columns] << "\t";
			cout << mul[rows][columns] << "\t";
			cout << mod[rows][columns] << "\t";
			cout << div[rows][columns] << "\t\n";
		}
	}

 	return 0;
}
Please Enter the rows and Columns of a Multi-Dimensional Array =  2 2

Please Enter the First Multi-Dimensional Array Items =  
10 20
30 40

Please Enter the Second Multi-Dimensional Array Items =  
15 9
22 50

The Result of the 1 Row Iteration

The Result of the 1 Column Iteration
10 + 15 = 25
10 - 15 = -5
10 * 15 = 150
10 / 15 = 0
10 % 15 = 10

The Result of the 2 Column Iteration
20 + 9 = 29
20 - 9 = 11
20 * 9 = 180
20 / 9 = 2
20 % 9 = 2

The Result of the 2 Row Iteration

The Result of the 1 Column Iteration
30 + 22 = 52
30 - 22 = 8
30 * 22 = 660
30 / 22 = 1
30 % 22 = 8

The Result of the 2 Column Iteration
40 + 50 = 90
40 - 50 = -10
40 * 50 = 2000
40 / 50 = 0
40 % 50 = 40

Add	Sub	Mul	Div	Mod
25	-5	150	0	10	
29	11	180	2	2	
52	8	660	1	8	
90	-10	2000	0	40