JosepRivaille

P37390: Producte de matrius quadrades

May 21st, 2015
1,676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef vector<int> Columna;
  6. typedef vector<Columna> Matriu;
  7.  
  8. Matriu producte(const Matriu& a, Matriu& b) {
  9.     int tam = a.size();
  10.     Matriu Producte(tam, Columna(tam));
  11.     for (int i = 0; i < tam; ++i) {
  12.         for (int j = 0; j < tam; ++j) {
  13.             int aux = 0;
  14.             for (int k = 0; k < tam; ++k) {
  15.                 aux += a[i][k] * b[k][j];
  16.             }
  17.             Producte[i][j] = aux;
  18.         }
  19.     }
  20.     return Producte;
  21. }
  22.  
  23. int main() {
  24.     int f, c;
  25.     cin >> f >> c;
  26.     Matriu a(f, Columna(c));
  27.     Matriu b(f, Columna(c));
  28.     for (int i = 0; i < f; ++i) {
  29.         for (int j = 0; j < c; ++j) {
  30.             cin >> a[i][j];
  31.         }
  32.     }
  33.     for (int i = 0; i < f; ++i) {
  34.         for (int j = 0; j < c; ++j) {
  35.             cin >> b[i][j];
  36.         }
  37.     }
  38.     for (int i = 0; i < f; ++i) {
  39.         for (int j = 0; j < c; ++j) {
  40.             cout << producte(a, b)[i][j] << " ";
  41.         }
  42.         cout << endl;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment