Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void sparseMultiplication(vector<vector<int>> firstMatrix, vector<vector<int>> secondMatrix);
- void sparseMultiplication(vector<vector<int>> firstMatrix, vector<vector<int>> secondMatrix)
- {
- if(firstMatrix[0].size() != secondMatrix.size())
- {
- cout<<"Multiplication is not possible"<<endl;
- return;
- }
- vector<vector<int>> resultMatrix(firstMatrix.size(),vector<int>(secondMatrix[0].size()));
- for(int i = 0; i < firstMatrix.size();i++)
- {
- for(int j = 0; j < firstMatrix[0].size(); j++)
- {
- if(firstMatrix[i][j] != 0)
- {
- for(int k = 0; k < secondMatrix[0].size(); k++)
- {
- if(secondMatrix[j][k] != 0 )
- {
- resultMatrix[i][j] += firstMatrix[i][j] * secondMatrix[j][k];
- }
- }
- }
- }
- }
- for(int i=0;i<resultMatrix.size();i++)
- {
- cout<<"{ ";
- for(int j=0;j<resultMatrix[0].size();j++)
- {
- cout<<resultMatrix[i][j];
- }
- cout<<" }"<<endl;
- }
- }
- int main()
- {
- vector<vector<int>> firstMatrix =
- {
- {1, 0, 0},
- {-1, 0, 3}
- };
- vector<vector<int>> secondMatrix =
- {
- {7, 0, 0},
- {0, 0, 0},
- {0, 0, 1}
- };
- sparseMultiplication(firstMatrix,secondMatrix);
- return 0;
- }
Add Comment
Please, Sign In to add comment