Updated March 28, 2023
Introduction to Matrix multiplication in C++
Matrix multiplication in C++ is a binary operation in which two matrices can be added, subtracted and multiplied. Input for row number, column number, first matrix elements, and second matrix elements is taken from the consumer to multiply the matrices. Then the matrices entered by the consumer are multiplied.
Examples of Matrix Multiplication
The examples of the following are given below:
Example #1
Code:
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],m,c,p,j,k;
cout<<" Enter the number of printing the rows=";
cin>>m;
cout<<"Enter the number of printing the column=";
cin>>c;
cout<<"Enter the first matrix of element=\n";
for(p=0;p<m;p++)
{
for(j=0;j<c;j++)
{
cin>>a[p][j];
}
}
cout<<"Enter the second matrix of element=\n";
for(p=0;p<m;p++)
{
for(j=0;j<c;j++)
{
cin>>b[p][j];
}
}
cout<<"multiply of the matrix=\n";
for(p=0;p<m;p++)
{
for(j=0;j<c;j++)
{
mul[p][j]=0;
for(k=0;k<c;k++)
{
mul[p][j]+=a[p][k]*b[k][j];
}
}
}
//for printing result
for(p=0;p<m;p++)
{
for(j=0;j<c;j++)
{
cout<<mul[p][j]<<" ";
}
cout<<"\n";
}
return 0;
}
Output:
Example #2
In this example, you are going to see the C++ program for two rectangular matrices two rectangular matrices
Code:
#include<bits/stdc++.h>
using namespace std;
// Multiplies 2 matrices first is matricsA[][] and second is matricsB[][] and prints result.
// (a1) x (a2) and (b1) x (b2) are
// dimensions of given matrices.
void multiply(int a1, int a2, int matA[][2],
int b1, int b2, int matB[][2])
{
int x, i, j;
int res[a1][b2];
for (i = 0; i < a1; i++)
{
for (j = 0; j < b2; j++)
{
res[i][j] = 0;
for (x = 0; x < a2; x++)
{
*(*(res + i) + j) += *(*(matA + i) + x) *
*(*(matB + x) + j);
}
}
}
for (i = 0; i < a1; i++)
{
for (j = 0; j < b2; j++)
{
cout << *(*(res + i) + j) << " ";
}
cout << "\n";
}
}
// Driver code
int main()
{
int matA[][2] = { { 4, 4 }, { 3, 4 } };
int matB[][2] = { { 2, 2 }, { 2, 3 } };
int a1 = 2, a2 = 2, b1 = 2, b2 = 2;
multiply(a1, a2, matA, b1, b2, matB);
return 0;
}
Output:
Example #3
In this example, you are going to the see C++ program two Square matrices
Code:
#include <iostream>
using namespace std;
#define N 4
// This function will multiplies A1[][] and B2[][], and it will stores the result in this res[][]
void multiply(int A1[][N],
int B2[][N],
int res[][N])
{
int a, z, k;
for (a = 0; a < N; a++)
{
for (z = 0; z < N; z++)
{
res[a][z] = 0;
for (k = 0; k < N; k++)
res[a][z] += A1[a][k] *
B2[k][z];
}
}
}
// Driver Code
int main()
{
int a, z;
int res[N][N]; // this is use to store result
int A1[N][N] = {{5, 5, 5, 5},
{6, 6, 6, 6},
{7, 7, 7, 7},
{8, 8, 8, 8}};
int B2[N][N] = {{5, 5, 5, 5},
{6, 6, 6, 6},
{7, 7, 7, 7},
{8, 8, 8, 8}};
multiply(A1, B2, res);
cout << "Resulted matrix are as follow \n";
for (a = 0; a < N; a++)
{
for (z = 0; z < N; z++)
cout << res[a][z] << " ";
cout << "\n";
}
return 0;
}
Output:
Example #4
Code:
#include<iostream>
using namespace std;
int main ()
{
int F1, c1, F2, c2, i, j, k;
int A[5][5], B[5][5], C[5][5];
cout << "Enter the number of columns and rows of matrix A : ";
cin >> F1 >> c1;
cout << "Enter number of Cloumns and rows of matrix B : ";
cin >> F2 >> c2;
if (c1 != F2)
{
cout << "Matrices can't be multiplied..";
exit(0);
}
cout << "Input the elements of matrix A : ";
for (i = 0; i < F1; i++)
for (j = 0; j < c1; j++)
cin >> A[i][j];
cout << "Input the elements of matrix B : ";
for (i = 0; i < F2; i++)
for (j = 0; j < c2; j++)
cin >> B[i][j];
for (i = 0; i < F1; i++)
{
for (j = 0; j < c2; j++)
{
C[i][j] = 0;
for (k = 0; k < F2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product of matrices\n";
for (i = 0; i < F1; i++)
{
for (j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << "\n";
}
return 0;
}
Output:
Explanation of program
The user is asked to enter the matrix A and matrix B rows and columns. If matrix A’s number of columns doesn’t suit matrix B’s number, matrices can’t be multiplied. Similarly, matrices for loops are combined and the result is placed in matrix C if they are equal.
Recommended Articles
This is a guide to Matrix Multiplication in C++. Here we discuss the introduction and the examples of Matrix Multiplication along with the codes & outputs. You can also go through our other suggested articles to learn more –