Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Matrix multiplication
- // Date : 19.10.19
- #include<iostream>
- using namespace std;
- int main(){
- cout << "Enter the row: ";
- int n; cin >> n;
- cout << "Enter the column: ";
- int m; cin >> m;
- int a[n][m], b[n][m], c[n][m];
- cout << "Enter the elements of first matrix: ";
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cin >> a[i][j];
- }
- }
- cout << "Enter the elements of second matrix: ";
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cin >> b[i][j];
- }
- }
- // Multiplication of 1st and 2nd matrix
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- c[i][j] = 0;
- for(int k = 0; k < n; k++){
- c[i][j] += (a[i][k] * b[k][j]);
- }
- }
- }
- // Output the matrix
- cout << "The first matrix is: " << endl;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cout << a[i][j] << "\t";
- }
- cout << endl;
- }
- cout << "The second matrix is: " << endl;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cout << b[i][j] << "\t";
- }
- cout << endl;
- }
- cout << "The multiplied matrix is: " << endl;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- cout << c[i][j] << "\t";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement