wiktortokumpel

macierze wektorowskie

Mar 22nd, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Macierz
  6. {
  7.     int m;
  8.     int n;
  9.     int **tab;
  10.  
  11. public:
  12.     Macierz (int M, int N)
  13.     {
  14.         m = M;
  15.         n = N;
  16.         alokuj();
  17.  
  18.     }
  19.     void alokuj()
  20.     {
  21.         tab =new int*[m];         //dla tablicy 3d byłoby ***tab;
  22.         for (int i = 0; i < m; ++i)
  23.         {
  24.             tab[i]=new int[n];
  25.         }
  26.     }
  27.     void wczytaj()
  28.     {
  29.         for (int i = 0; i < m; ++i)
  30.         {
  31.             for (int j = 0; j < n; ++j) {
  32.                 cin>>tab[i][j];
  33.             }
  34.  
  35.         }
  36.     }
  37.     void wyswietl()
  38.     {
  39.         for (int i = 0; i < m; ++i)
  40.         {
  41.             for (int j = 0; j < n; ++j) {
  42.                 cout<<tab[i][j]<<" ";
  43.             }
  44.             cout<<endl;
  45.         }
  46.     }
  47.  
  48.     Macierz dodaj (Macierz &b)
  49.     {
  50.         Macierz c={m,n};
  51.         c.alokuj();
  52.  
  53.         for(int i=0;i<m;i++){
  54.             for (int j = 0; j < n; ++j) {
  55.                 c.tab[i][j]=tab[i][j]+b.tab[i][j];
  56.             }
  57.         }
  58.         return c;
  59.     };
  60.     Macierz odejmij (Macierz &b)
  61.     {
  62.         Macierz d={m,n};
  63.         d.alokuj();
  64.  
  65.         for(int i=0;i<m;i++){
  66.             for (int j = 0; j < n; ++j) {
  67.                 d.tab[i][j]=tab[i][j]-b.tab[i][j];
  68.             }
  69.         }
  70.         return d;
  71.     };
  72.  
  73. };
  74.  
  75.  
  76.  
  77. int main()
  78. {
  79.     Macierz a(2,2);
  80.     a.alokuj();
  81.     Macierz b(2,2);
  82.     b.alokuj();
  83.  
  84. //wczytanie funkcji
  85.     cout<<"podaj wartosc tablicy 1: ";
  86.     cout<<endl;
  87.     a.wczytaj();
  88.     cout<<endl;
  89.     cout<<"podaj wartosc tablicy 2: ";
  90.     cout<<endl;
  91.     b.wczytaj();
  92.  
  93. //Wyświetlanie funkcji
  94.     cout<<"tablica 1 zawiera liczby: ";
  95.     cout<<endl;
  96.     a.wyswietl();
  97.  
  98.     cout<<"tablica 2 zawiera liczby: ";
  99.     cout<<endl;
  100.     b.wyswietl();
  101.  
  102.     Macierz c=a.dodaj(b);
  103.     cout<<"Macierz wynikowa dodawania:"<<endl;
  104.     c.wyswietl();
  105.  
  106.     Macierz d=a.odejmij(b);
  107.     cout<<"Macierz wynikowa odejmowania:"<<endl;
  108.     d.wyswietl();
  109.  
  110.     return 0;
  111. }
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment