Matrix multiplication: Write a C++ program to compute the product of two matrice
ID: 3669980 • Letter: M
Question
Matrix multiplication: Write a C++ program to compute the product of two matrices of doubles. You are required to use the template class vector to represent a matrix. (Seevector_matrix.cpp on the main course page, which is an example of how to create and initialize a 2d-matrix using vectors.) Specifically, your program will include the main( ) function and a second function multiply_matrices( ). The main() function will
- allow the end-users to provide the dimensionality of the two matrices A and B, and subsequently the content of A and B
- call the multiply_matrices() function to compute the product of A and B
- print out the multiplication result
Explanation / Answer
// MatrixMultiplication.cpp
#include<iostream>
#include<vector>
using namespace std;
vector<vector<double>> multiply_matrix(vector<vector<double>> matrix1, vector<vector<double>> matrix2);
int main()
{
vector<vector<double>> a;
vector<vector<double>> b;
int row_a = 0, column_a = 0, row_b = 0, column_b = 0;
cout << "Enter the dimensions of matrix, first enter the number of rows followed by number of columns" << endl;
cin >> row_a >> column_a;
cout << "enter the elements" << endl;
double input = 0;
for (int i = 0; i < row_a; i++)
{
//taking a temporary vector for each row input
vector<double> temp;
for (int j = 0; j < column_a; j++)
{
cin >> input;
temp.push_back(input);
}
//pushing a whole row vector to a vector of vectors
a.push_back(temp);
}
cout << "Enter the size of second matrix, first enter the number of rows followed by number of columns" << endl;
cin >> row_b >> column_b;
cout << "enter the elements" << endl;
for (int i = 0; i < row_b; i++)
{
vector<double> temp;
for (int j = 0; j < column_b; j++)
{
cin >> input;
temp.push_back(input);
}
b.push_back(temp);
}
//if num of columns of first matrix and num of rows of second matrix are not equal we cannot multiply the matrices
if (column_a != row_b)
{
cout << "two matrices cannot be multiplied as num of column of first matrix are not equal to num of rows of second" << endl;
}
else
{
vector<vector<double>> c = multiply_matrix(a, b);
//displaying the product matrix
cout << "Product of two matrix is" << endl;
for (int i = 0; i < row_a; ++i)
{
for (int j = 0; j < column_b; ++j)
{
cout << c[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
vector<vector<double>> multiply_matrix(vector<vector<double>> a, vector<vector<double>> b)
{
vector<vector<double>> c;
for (int i = 0; i < a.size(); ++i)
{
//temp vector to store the each row of new matrix c
vector<double> temp;
for (int j = 0; j < b[0].size(); ++j)
{
double sum = 0;
for (int k = 0; k < b.size(); ++k)
{
sum+=a[i][k] * b[k][j];
}
//pushing the each element after each row by column product into the row vector of produt matrix
temp.push_back(sum);
}
//pushing the whole row vector into product vector c which is a vector of vectors
c.push_back(temp);
}
return c;
}
sample output:
Enter the dimensions of matrix, first enter the number of rows followed by numbe
r of columns
2
3
enter the elements
1.2
3.2
4
5
6.5
0
Enter the size of second matrix, first enter the number of rows followed by numb
er of columns
3
2
enter the elements
2.5
6.3
1.1
6
4
7
Product of two matrix is
22.52 54.76
19.65 70.5