Hydron

Untitled

Feb 8th, 2022 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Matrix{
  6. private:
  7.     int n, m;
  8.     int **a;
  9. public:
  10.     Matrix(int rows=2, int columns=2, int value=0){
  11.         n = rows, m = columns;
  12.         a = new int*[n];
  13.         for (int i=0; i<n; i++){
  14.             a[i] = new int[m];
  15.             for (int j=0; j<m; j++) a[i][j] = value;
  16.         }
  17.     }
  18.     void place(int row, int column, int val){
  19.         a[row][column] = val;
  20.     }
  21.     int *get_all_as_array(){
  22.         int *return_value = new int[n*m];
  23.         for (int i=0; i<n; i++)
  24.             for (int j=0; j<m; j++)
  25.                 return_value[m*i+j] = a[i][j];
  26.         return return_value;
  27.     }
  28.     void print_content(){
  29.         for (int i=0; i<n; i++){
  30.             for (int j=0; j<m; j++)
  31.                 cout << a[i][j] << ' ';
  32.             cout << endl;
  33.         }
  34.     }
  35.     pair<int,int> get_dimensions(){
  36.         return {n, m};
  37.     }
  38. };
  39.  
  40. int main(){
  41.     Matrix m1 = Matrix(1, 10, 1), m2 = Matrix(2, 3, 12), m3 = Matrix();
  42.     cout << "Matrices:\n";
  43.     m1.print_content();
  44.     cout << endl;
  45.     m2.print_content();
  46.     cout << endl;
  47.     m3.print_content();
  48.     int *arr1 = m1.get_all_as_array(), n1 = m1.get_dimensions().first * m1.get_dimensions().second;
  49.     int *arr2 = m2.get_all_as_array(), n2 = m2.get_dimensions().first * m2.get_dimensions().second;
  50.     int *arr3 = m3.get_all_as_array(), n3 = m3.get_dimensions().first * m3.get_dimensions().second;
  51.     cout << "\nArrays:\n";
  52.     for (int i=0; i<n1; i++){
  53.         cout << arr1[i] << ' ';
  54.     }
  55.     cout << endl << endl;
  56.     for (int i=0; i<n2; i++){
  57.         cout << arr2[i] << ' ';
  58.     }
  59.     cout << endl << endl;
  60.     for (int i=0; i<n3; i++){
  61.         cout << arr3[i] << ' ';
  62.     }
  63.     cout << endl << endl;
  64.     m2.place(1, 2, -5);
  65.     cout << "Matrix2 after placing (-5) in position (1, 2):\n";
  66.     m2.print_content();
  67.     return 0;
  68. }
  69.  
Add Comment
Please, Sign In to add comment