Multiplication of two matrix in c++

C++ program for Multiplication of two matrix. Here mentioned other language solution.
// Include header file
#include <iostream>
#include <vector>
using namespace std;
// stdc++ 11 program for
// Two matrix multiplication
class Multiply {
// Display the element of given 2d matrix
public :
static void printRecord(vector<vector<int>> matrix)
{
cout<<" --------------"<<endl;
// Assume N x N Matrix size
int row = matrix.size();
int col = matrix[0].size();
// Iterate the row element
for (int i = 0; i < row; ++i) {
// Iterate the column element
for (int j = 0; j < col; ++j) {
// Display element value
cout<<" " << matrix[i][j];
}
// Add new line
cout<<endl;
}
cout<<endl;
}
static void multiplication(vector<vector<int>> a, vector<vector<int>> b)
{
// Get the size
int row = a.size();
int col = a[0].size();
// This matrix are store the result of multiplication
vector<vector<int>> result(row,vector<int>(col,0));
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
// Set the initial value of new matrix element
result[i][j] = 0;
for (int k = 0; k < row; ++k) {
// Multiply matrix A [i] row and [k] columns to
// the Matrix B [k] columns and [j] rows.
result[i][j] += a[i][k] * b[k][j];
}
}
}
cout<<" Matrix A"<<endl;
// Print element of matrix x
Multiply::printRecord(a);
cout<<" Matrix B"<<endl;
// Print element of matrix y
Multiply::printRecord(b);
cout<<" Matrix [(A) x (B)]"<<endl;
// Display resultant matrix
Multiply::printRecord(result);
}
};
int main()
{
// Define matrix A
vector<vector<int>> a {
{1, 2, 3},
{6, 1, 2},
{5, 4, 3}
};
// Define matrix B
vector<vector<int>> b {
{3, 1, 3},
{1, 1, 2},
{2, 2, 3}
};
Multiply::multiplication(a, b);
}
Output
Matrix A
--------------
1 2 3
6 1 2
5 4 3
Matrix B
--------------
3 1 3
1 1 2
2 2 3
Matrix [(A) x (B)]
--------------
11 9 16
23 11 26
25 15 32
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment