Advertisement
skb50bd

Matrix Multiplication w/o Limit

Mar 17th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int Ar, Ac, Br, Bc;
  7.  
  8.     cout << "Enter the size of \"A\" matrix (x * y): ";
  9.     cin >> Ar >> Ac;
  10.     cout << "Enter the size of \"B\" matrix (y * z): ";
  11.     cin >> Br >> Bc;
  12.  
  13.     if (Ac != Br) {
  14.         cout << "Error, size of the Matrices are not satisfiable;" << endl;
  15.         exit(1);
  16.     }
  17.  
  18.     else {
  19.         int A[Ar][Ac], B[Br][Bc], C[Ar][Bc];
  20.  
  21.         cout << "Enter the A-Matrix (x * y elements): ";
  22.         for (int i = 0; i < Ar; i++)
  23.             for (int j = 0; j < Ac; j++)
  24.                 cin >> A[i][j];
  25.  
  26.         cout << "Enter the B-Matrix (y * z elements): ";
  27.         for (int i = 0; i < Br; i++)
  28.             for (int j = 0; j < Bc; j++)
  29.                 cin >> B[i][j];
  30.  
  31.         for (int i = 0; i < Ar; i++)
  32.             for (int j = 0, sum = 0; j < Bc; j++) {
  33.                 for (int k = 0; k < Ac; k++)
  34.                     sum += (A[i][k] * B[k][j]);
  35.                 C[i][j] = sum;
  36.                 sum = 0;
  37.             }
  38.  
  39.         cout << "Printing the Product of A and B Matrices: " << endl;
  40.         for (int i = 0; i < Ar; i++) {
  41.             for (int j = 0; j < Bc; j++)
  42.                 cout << C[i][j] << "\t";
  43.             cout << endl;
  44.         }
  45.         cout << endl;
  46.     }
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement