Advertisement
fahimkamal63

Matrix multiplication

Oct 18th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. //  Matrix multiplication
  2. //  Date : 19.10.19
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. int main(){
  7.     cout << "Enter the row: ";
  8.     int n; cin >> n;
  9.     cout << "Enter the column: ";
  10.     int m; cin >> m;
  11.     int a[n][m], b[n][m], c[n][m];
  12.     cout << "Enter the elements of first matrix: ";
  13.     for(int i = 0; i < n; i++){
  14.         for(int j = 0; j < m; j++){
  15.             cin >> a[i][j];
  16.         }
  17.     }
  18.     cout << "Enter the elements of second matrix: ";
  19.     for(int i = 0; i < n; i++){
  20.         for(int j = 0; j < m; j++){
  21.             cin >> b[i][j];
  22.         }
  23.     }
  24.  
  25.     //  Multiplication of 1st and 2nd matrix
  26.     for(int i = 0; i < n; i++){
  27.         for(int j = 0; j < m; j++){
  28.                 c[i][j] = 0;
  29.             for(int k = 0; k < n; k++){
  30.                 c[i][j] += (a[i][k] * b[k][j]);
  31.             }
  32.         }
  33.     }
  34.  
  35.     //  Output the matrix
  36.     cout << "The first matrix is: " << endl;
  37.     for(int i = 0; i < n; i++){
  38.         for(int j = 0; j < m; j++){
  39.             cout << a[i][j] << "\t";
  40.         }
  41.         cout << endl;
  42.     }
  43.     cout << "The second matrix is: " << endl;
  44.     for(int i = 0; i < n; i++){
  45.         for(int j = 0; j < m; j++){
  46.             cout << b[i][j] << "\t";
  47.         }
  48.         cout << endl;
  49.     }
  50.     cout << "The multiplied matrix is: " << endl;
  51.     for(int i = 0; i < n; i++){
  52.         for(int j = 0; j < m; j++){
  53.             cout << c[i][j] << "\t";
  54.         }
  55.         cout << endl;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement