skioe

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

Apr 1st, 2021 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 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 Show1DIM(int*, size_t);
  10. void NewMassGen(int**, size_t, size_t);
  11. void Kill(int**, size_t);
  12.  
  13. int main()
  14. {
  15.     setlocale(LC_ALL, "Russian");
  16.  
  17.     int** Matrix = NULL;
  18.     size_t Rows, Columns;
  19.  
  20.     cout << "Введите кол-во строк: ";
  21.     cin >> Rows;
  22.  
  23.     cout << "Введите кол-во столбцов: ";
  24.     cin >> Columns;
  25.  
  26.     Matrix = CreateMatrix(Matrix, Rows, Columns);
  27.  
  28.     Show(Matrix, Rows, Columns);
  29.     NewMassGen(Matrix, Rows, Columns);
  30.     Kill(Matrix, Rows);
  31.  
  32.     return 0;
  33. }
  34.  
  35. int** CreateMatrix(int** Matrix, size_t Rows, size_t Columns) {
  36.  
  37.     Matrix = new int* [Rows];
  38.  
  39.     cout << "\nЗаполните массив:\n";
  40.  
  41.     for (size_t i = 0; i < Rows; i++) // Указатель i
  42.     {
  43.         Matrix[i] = new int[Columns]; // Выделяем память
  44.  
  45.         for (size_t j = 0; j < Columns; j++) // Цикл, заполняющий массив / указатель j
  46.         {
  47.             cout << "Массив [" << i + 1 << "][" << j + 1 << "] = "; // Показываем, какой элемент сейчас вводим
  48.             cin >> Matrix[i][j]; // Вводим этот элемент
  49.  
  50.         }
  51.     }
  52.     return Matrix;
  53. }
  54.  
  55. void Show(int** Matrix, size_t Rows, size_t Columns) {
  56.    
  57.     cout << "\nВведенный массив:" << endl;
  58.     for (size_t i = 0; i < Rows; i++, cout << endl)
  59.         for (size_t j = 0; j < Columns; j++)
  60.             cout << Matrix[i][j] << "\t"; // Выводим введенный массив
  61. }
  62.  
  63. void Show1DIM(int* Matrix, size_t Columns) {
  64.  
  65.     cout << "\nПолученный массив:" << endl;
  66.     for (size_t i = 0; i < Columns; i++)
  67.             cout << Matrix[i] << "\t"; // Выводим полученный массив
  68. }
  69.  
  70. void Kill(int** Matrix, size_t Rows) {
  71.    
  72.     for (size_t i = 0; i < Rows; i++)
  73.         delete[] Matrix[i];
  74.  
  75.     delete[] Matrix;
  76. }
  77.  
  78. void NewMassGen(int** Matrix, size_t Rows, size_t Columns) { // Ищем данные по заданию
  79.  
  80.     int elsum, max_in_row, * TaskArray;
  81.  
  82.     elsum = 0;
  83.  
  84.     if (Rows >= Columns)
  85.         TaskArray = new int[Columns]();
  86.     else
  87.         TaskArray = new int[Rows]();
  88.    
  89.     for (size_t i = 0; i < Rows; i++)
  90.     {
  91.         max_in_row = Matrix[i][i];
  92.         for (size_t j = 0; j < Columns; j++)
  93.         {
  94.             if (Matrix[i][j] > max_in_row)
  95.                 max_in_row = Matrix[i][j];
  96.  
  97.             elsum += Matrix[i][j];
  98.         }
  99.  
  100.         if (Matrix[i][i] == max_in_row) {
  101.             TaskArray[i] = 1;
  102.         }
  103.            
  104.     }
  105.  
  106.     if (Rows >= Columns)
  107.         Show1DIM(TaskArray, Columns);
  108.     else
  109.         Show1DIM(TaskArray, Rows);
  110.  
  111.     cout << "\n\nСумма элементов матрицы = " << elsum << endl;
  112.    
  113.     delete[] TaskArray;
  114. }
Add Comment
Please, Sign In to add comment