Matrix multiplication: Write a C++ program to compute the product of two matrice
ID: 670157 • Letter: M
Question
Matrix multiplication: Write a C++ program to compute the product of two matrices. You are required to use the template class vector to represent a matrix. (See 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
#include<cstdlib>
#include <iostream>
}
#include<cstdlib>
#include <iostream>
#include <vector> #include <iomanip> using namespace std; void showMatrix(vector <vector<int> > matrix) { for (int i = 0; i < matrix.size(); i++) // i is the row in the matrix { for (int j = 0; j < matrix[i].size(); j++) // j is the col in the matrix { cout << setw(3) << matrix[i][j]; } cout << endl; } cout << endl; } vector <vector<int> > vectorMultiply(vector <vector<int> > &matrix1, vector<vector<int> > &matrix2) { vector<vector<int> > result; int sum = 0; for (int i = 0; i < matrix1.size(); i++) { vector<int> temp; for (int j = 0; j < matrix1[i].size(); j++) { for (int k = 0; k < matrix1[i*j].size(); k++) //error { sum = sum + matrix1[i][k] * matrix2[k][j]; temp.push_back(sum); } sum = 0; result.push_back(temp); } } return result; } int main() { int inputSizeFMRow = 0; //input for size of first matrix row int inputSizeFMCol = 0; //input for size of first matrix col int inputSizeSMRow = 0; //input for size of second matrix row int inputSizeSMCol = 0; //input for size of second matrix col int userInput = 0; //elements provided by the user vector <vector<int> > firstMatrix(inputSizeFMRow); vector <vector<int> > secondMatrix(inputSizeSMRow); cout << "Please enter the row size of the first matrix: "; cin >> inputSizeFMRow; cout << "Please enter the column size of the first matrix: "; cin >> inputSizeFMCol; cout << endl; for (int i = 0; i < inputSizeFMRow; i++) { vector<int> temp; for (int j = 0; j < inputSizeFMCol; j++) { cout << "Please enter values into the first matrix: "; cin >> userInput; temp.push_back(userInput); } firstMatrix.push_back(temp); } cout << " Please enter the row size of the second matrix: "; cin >> inputSizeSMRow; cout << "Please enter the column size of the second matrix: "; cin >> inputSizeSMCol; cout << endl; if (inputSizeFMCol != inputSizeSMRow) { cout << "These matrices cannot be multiplied together. "; } else { for (int i = 0; i < inputSizeSMRow; i++) { vector<int> temp; for (int j = 0; j < inputSizeSMCol; j++) { cout << "Now please enter the values for the second matrix: "; cin >> userInput; temp.push_back(userInput); } secondMatrix.push_back(temp); } vector <vector<int> > multiMatrix = vectorMultiply(firstMatrix, secondMatrix); showMatrix(multiMatrix); } return 0;}