Advertisement
niks32

NikitaLearn4.1

Nov 21st, 2022
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. // ConsoleNikitaLearn4.1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class Matrix
  10. {
  11. public:
  12.     Matrix(int countRow, int countCol)
  13.     {
  14.         this->countCol = countCol;
  15.         this->countRow = countRow;
  16.  
  17.         arr = new double* [countRow];
  18.         for (int i = 0; i < countRow; i++)
  19.         {
  20.             arr[i] = new double[countCol];
  21.             //сразу обнуляем значения;
  22.             for (int j = 0; j < countCol; j++)
  23.             {
  24.                 arr[i][j] = 0;
  25.             }
  26.         }      
  27.     }
  28.  
  29.     double get_element(int rowIndex, int colIndex)
  30.     {
  31.         return arr[rowIndex][colIndex];
  32.     }
  33.  
  34.     void set_element(int rowIndex, int colIndex, double value)
  35.     {
  36.         arr[rowIndex][colIndex] = value;
  37.     }
  38.  
  39.     void printArr()
  40.     {
  41.         for (int i = 0; i < countRow; i++)
  42.         {
  43.             for (int j = 0; j < countCol; j++)
  44.             {
  45.                 cout << arr[i][j] << "\t";
  46.             }
  47.             cout << endl;
  48.         }
  49.     }
  50.    
  51.     void addition(Matrix &arr2, Matrix &result) //сложние двух матриц (нету проверки на размерность)
  52.     {
  53.         double tmp;
  54.         for (int i = 0; i < countRow; i++)
  55.         {
  56.             for (int j = 0; j < countCol; j++)
  57.             {
  58.                 tmp = this->arr[i][j] + (arr2.get_element(i, j));
  59.                 result.set_element(i, j, tmp);
  60.             }
  61.         }
  62.     }
  63.        
  64. private:
  65.     double** arr;
  66.     int countRow;
  67.     int countCol;
  68. };
  69.  
  70. void setTestData1(Matrix& my_matrix1)
  71. {
  72.     int counter = 0;
  73.     for (int i = 0; i < 3; i++)
  74.     {
  75.         for (int j = 0; j < 3; j++)
  76.         {
  77.             my_matrix1.set_element(i, j, counter);
  78.             counter++;
  79.         }
  80.     }
  81. }
  82.  
  83. void setTestData2(Matrix& my_matrix1)
  84. {
  85.     int counter = 99;
  86.     for (int i = 0; i < 3; i++)
  87.     {
  88.         for (int j = 0; j < 3; j++)
  89.         {
  90.             my_matrix1.set_element(i, j, counter);
  91.             counter--;
  92.         }
  93.     }
  94. }
  95.  
  96. int main()
  97. {
  98.     setlocale(LC_ALL, "Ru");
  99.  
  100.     Matrix my_matrix1 = Matrix(3, 3);
  101.     Matrix my_matrix2 = Matrix(3, 3);
  102.     Matrix result = Matrix(3, 3);
  103.  
  104.     cout << "setTestData1(my_matrix1)-----------------------" << endl;
  105.     setTestData1(my_matrix1);
  106.     my_matrix1.printArr();
  107.  
  108.     cout << "setTestData1(my_matrix2)-----------------------" << endl;
  109.     setTestData2(my_matrix2);
  110.     my_matrix2.printArr();
  111.  
  112.     my_matrix1.addition(my_matrix2, result);
  113.     cout << "setTestData1(my_matrix2)-----------------------" << endl;
  114.     result.printArr();
  115.  
  116.  
  117.     std::cout << "Hello World!\n";
  118. }
  119.  
  120. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  121. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement