Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleNikitaLearn4.1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include <iostream>
- using namespace std;
- class Matrix
- {
- public:
- Matrix(int countRow, int countCol)
- {
- this->countCol = countCol;
- this->countRow = countRow;
- arr = new double* [countRow];
- for (int i = 0; i < countRow; i++)
- {
- arr[i] = new double[countCol];
- //сразу обнуляем значения;
- for (int j = 0; j < countCol; j++)
- {
- arr[i][j] = 0;
- }
- }
- }
- double get_element(int rowIndex, int colIndex)
- {
- return arr[rowIndex][colIndex];
- }
- void set_element(int rowIndex, int colIndex, double value)
- {
- arr[rowIndex][colIndex] = value;
- }
- void printArr()
- {
- for (int i = 0; i < countRow; i++)
- {
- for (int j = 0; j < countCol; j++)
- {
- cout << arr[i][j] << "\t";
- }
- cout << endl;
- }
- }
- void addition(Matrix &arr2, Matrix &result) //сложние двух матриц (нету проверки на размерность)
- {
- double tmp;
- for (int i = 0; i < countRow; i++)
- {
- for (int j = 0; j < countCol; j++)
- {
- tmp = this->arr[i][j] + (arr2.get_element(i, j));
- result.set_element(i, j, tmp);
- }
- }
- }
- private:
- double** arr;
- int countRow;
- int countCol;
- };
- void setTestData1(Matrix& my_matrix1)
- {
- int counter = 0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- my_matrix1.set_element(i, j, counter);
- counter++;
- }
- }
- }
- void setTestData2(Matrix& my_matrix1)
- {
- int counter = 99;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- my_matrix1.set_element(i, j, counter);
- counter--;
- }
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Ru");
- Matrix my_matrix1 = Matrix(3, 3);
- Matrix my_matrix2 = Matrix(3, 3);
- Matrix result = Matrix(3, 3);
- cout << "setTestData1(my_matrix1)-----------------------" << endl;
- setTestData1(my_matrix1);
- my_matrix1.printArr();
- cout << "setTestData1(my_matrix2)-----------------------" << endl;
- setTestData2(my_matrix2);
- my_matrix2.printArr();
- my_matrix1.addition(my_matrix2, result);
- cout << "setTestData1(my_matrix2)-----------------------" << endl;
- result.printArr();
- std::cout << "Hello World!\n";
- }
- // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
- // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement