C++ Program to Multiply two Matrixes

Write a C++ Program to Multiply two Matrixes with an example. In this C++ matrix multiplication example, within the C++ nested for loop, we multiplied the subarr1 matrix with subarr2 and assigned it to multiplication. Next, we used cout to print the multiplication matrix items.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the Multiply Multi-Dimensional Array rows & Columns =  ";
	cin >> i >> j;
	
	int multarr1[i][j], multarr2[i][j], multiplication[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> multarr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> multarr2[rows][columns];
		}		
	}
	cout << "\nThe Final Result after Multiplying multarr2 with multarr1\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			multiplication[rows][columns] = multarr1[rows][columns] * multarr2[rows][columns];
			cout << multiplication[rows][columns] << " ";
		}
	}

 	return 0;
}
C++ Program to Multiply two Matrixes 1

C++ Program to Multiply two Matrixes Example 2

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

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the Multiply Multi-Dimensional Array rows & Columns =  ";
	cin >> i >> j;
	
	int multarr1[i][j], multarr2[i][j], multiplication[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> multarr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> multarr2[rows][columns];
		}		
	}
	for(rows = 0; rows < i; rows++)	{
		cout << "\n---The Multiplication Result of " << rows + 1 << " Row Iteration---\n";
		for(columns = 0; columns < j; columns++) {
			multiplication[rows][columns] = multarr1[rows][columns] * multarr2[rows][columns];
			
			cout << "\nThe Multiplication Result of " << columns + 1 << " Column Iteration = ";
			cout << multarr1[rows][columns] << " * " << multarr2[rows][columns] << " = " << multiplication[rows][columns] << "\n";
		}
	}
	cout << "\nThe Final Result after Multiplying multarr2 with multarr1\n ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			cout << multiplication[rows][columns] << "  ";
		}
		cout<<"\n";
	}

 	return 0;
}
Please Enter the Multiply Multi-Dimensional Array rows & Columns =  2 2

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

Please Enter the Second Multi-Dimensional Array Items =  
3 4
5 6

---The Multiplication Result of 1 Row Iteration---

The Multiplication Result of 1 Column Iteration = 10 * 3 = 30

The Multiplication Result of 2 Column Iteration = 20 * 4 = 80

---The Multiplication Result of 2 Row Iteration---

The Multiplication Result of 1 Column Iteration = 30 * 5 = 150

The Multiplication Result of 2 Column Iteration = 40 * 6 = 240

The Final Result after Multiplying multarr2 with multarr1
 30  80  
150  240