Advertisement
barbos01

Untitled

Nov 12th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. class matrice {
  5.  public:
  6.   vector<vector<int>> a;
  7.   int line, coloana;
  8.   friend matrice operator+(matrice m1, matrice m2);
  9.    void afisare(matrice m);
  10. };
  11.  
  12. matrice operator+(matrice m1, matrice m2) {
  13.   matrice s;
  14.   s.line = m1.line;
  15.  
  16.   s.coloana = m1.coloana;
  17.   for (int i = 0; i < m1.a.size(); i++) {
  18.     vector<int> temp;
  19.     for (int j = 0; j < m1.a[i].size(); j++) {
  20.       temp.push_back(m1.a[i][j] + m2.a[i][j]);
  21.     }
  22.     s.a.push_back(temp);
  23.   }
  24.   return s;
  25. }
  26.  
  27. void afisare(matrice m) {
  28.   for (int i = 0; i < m.a.size(); i++) {
  29.     for (int j = 0; j < m.a[i].size(); j++) {
  30.       cout << m.a[i][j] << " ";
  31.     }
  32.     cout << endl;
  33.   }
  34. }
  35.  
  36. int main() {
  37.   // mesaj citire matrice 1
  38.   matrice m1;
  39.   cin >> m1.line >> m1.coloana;
  40.   for (int i = 0; i < m1.line; i++) {
  41.     vector<int> aux;
  42.     for (int j = 0; j < m1.coloana; j++) {
  43.       int n;
  44.       cin >> n;
  45.       aux.push_back(n);
  46.     }
  47.     m1.a.push_back(aux);
  48.   }
  49.  
  50. // mesaj citire matrice 2
  51.   matrice m2;
  52.   cin >> m2.line >> m2.coloana;
  53.   for (int i = 0; i < m2.line; i++) {
  54.     vector<int> aux;
  55.     for (int j = 0; j < m2.coloana; j++) {
  56.       int n;
  57.       cin >> n;
  58.       aux.push_back(n);
  59.     }
  60.     m2.a.push_back(aux);
  61.   }
  62.   // mesaj afisare rezultat adunare matrice 1 + matrice 2
  63.   matrice m;
  64.   m = m1 + m2;
  65.   afisare(m);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement