Advertisement
bero_0401

matrix multiplication

Jul 11th, 2024 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define R1 2 // number of rows in Matrix-1
  5. #define C1 2 // number of columns in Matrix-1
  6. #define R2 2 // number of rows in Matrix-2
  7. #define C2 3 // number of columns in Matrix-2
  8.  
  9. void mulMat(int mat1[][C1], int mat2[][C2])
  10. {
  11.     int rslt[R1][C2];
  12.     for (int i = 0; i < R1; i++) {
  13.         for (int j = 0; j < C2; j++) {
  14.             rslt[i][j] = 0;
  15.             for (int k = 0; k < R2; k++) {
  16.                 rslt[i][j] += mat1[i][k] * mat2[k][j];
  17.             }
  18.         }
  19.     }
  20. }
  21.  
  22. int main()
  23. {
  24.     // R1 = 4, C1 = 4 and R2 = 4, C2 = 4
  25.     int mat1[R1][C1] = { { 1, 1 }, { 2, 2 } };
  26.     int mat2[R2][C2] = { { 1, 1, 1 }, { 2, 2, 2 } };
  27.     if (C1 != R2) {
  28.         exit(EXIT_FAILURE);
  29.     }
  30.     mulMat(mat1, mat2);
  31.     return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement