skioe

Лабораторная 1, 3

Apr 1st, 2021 (edited)
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int** CreateMatrix(int**, size_t, size_t);
  8. void Show(int**, size_t, size_t);
  9. void NewMassGen(int**, size_t, size_t);
  10. void Kill(int**, size_t);
  11.  
  12. int main()
  13. {
  14.     setlocale(LC_ALL, "Russian");
  15.  
  16.     int** Matrix = NULL;
  17.     size_t Rows, Columns;
  18.  
  19.     cout << "Введите кол-во строк: ";
  20.     cin >> Rows;
  21.  
  22.     cout << "Введите кол-во столбцов: ";
  23.     cin >> Columns;
  24.  
  25.     Matrix = CreateMatrix(Matrix, Rows, Columns);
  26.  
  27.     Show(Matrix, Rows, Columns);
  28.     NewMassGen(Matrix, Rows, Columns);
  29.     Kill(Matrix, Rows);
  30.  
  31.     return 0;
  32. }
  33.  
  34. int** CreateMatrix(int** Matrix, size_t Rows, size_t Columns) {
  35.  
  36.     Matrix = new int* [Rows];
  37.  
  38.     cout << "\nЗаполните массив:\n";
  39.  
  40.     for (size_t i = 0; i < Rows; i++) // Указатель i
  41.     {
  42.         Matrix[i] = new int[Columns]; // Выделяем память
  43.  
  44.         for (size_t j = 0; j < Columns; j++) // Цикл, заполняющий массив / указатель j
  45.         {
  46.             cout << "Массив [" << i + 1 << "][" << j + 1 << "] = "; // Показываем, какой элемент сейчас вводим
  47.             cin >> Matrix[i][j]; // Вводим этот элемент
  48.  
  49.         }
  50.     }
  51.     return Matrix;
  52. }
  53.  
  54. void Show(int** Matrix, size_t Rows, size_t Columns) {
  55.    
  56.     cout << "\nВведенный массив:" << endl;
  57.     for (size_t i = 0; i < Rows; i++, cout << endl)
  58.         for (size_t j = 0; j < Columns; j++)
  59.             cout << Matrix[i][j] << "\t"; // Выводим введенный массив
  60. }
  61.  
  62. void Kill(int** Matrix, size_t Rows) {
  63.    
  64.     for (size_t i = 0; i < Rows; i++)
  65.         delete[] Matrix[i];
  66.  
  67.     delete[] Matrix;
  68. }
  69.  
  70. void NewMassGen(int** Matrix, size_t Rows, size_t Columns) { // Ищем данные по заданию
  71.  
  72.     float itemssum = 0;
  73.  
  74.     for (size_t i = 0; i < Rows; i++)
  75.     {
  76.         for (size_t j = 0; j < Columns; j++)
  77.         {
  78.             if (i == j)
  79.                 itemssum += Matrix[i][j];
  80.  
  81.             if (i < j and Matrix[i][j] < 0)
  82.                 Matrix[i][j] = abs(Matrix[i][j]);
  83.         }
  84.     }
  85.  
  86.     Show(Matrix, Rows, Columns);
  87.  
  88.     cout << "Сумма элементов главной диагонали = " << itemssum << endl;
  89. }
Add Comment
Please, Sign In to add comment