Guest User

Бла-бла-бла очередной запрос продавана кода.

a guest
Nov 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include "cstdlib"
  2. #include "locale"
  3. #include "iostream"
  4. #include "iomanip"
  5.  
  6. #define CONST_ABS_ELEM_VALUE 20
  7. #define DEFAULT_VALUE_SETW 5
  8.  
  9. int** initialize_new_matrix_with_random_values(int n, int m);
  10. void delete_matrix(int** matrix, int m);
  11. void output_full_matrix(int const n, int const m, int** const a, int setw_value);
  12. void init_io_threads();
  13. int find_max_sum_and_column_index_by_abs(int** const src_matrix, const int n, int & sum);
  14. int find_min_element_in_column(int ** const scr_matrix, const int n, const int i);
  15. void find_min_elem_of_max_sum_column();
  16.  
  17. int main()
  18. {
  19.     find_min_elem_of_max_sum_column();
  20. }
  21.  
  22. void find_min_elem_of_max_sum_column()
  23. {
  24.     srand(NULL);
  25.     init_io_threads();
  26.  
  27.     auto n = 12;
  28.  
  29.     std::wcout << L"Введите размерность матрицы: ";
  30.     std::cin >> n;
  31.     std::wcout << std::endl << L"Введена матрица " << n << L" x " << n << L"." << std::endl;
  32.     std::wcout << L"Матрица заполняется случайными значениями." << std::endl;
  33.  
  34.     const auto matrix = initialize_new_matrix_with_random_values(n, n);
  35.  
  36.     output_full_matrix(n, n, matrix, DEFAULT_VALUE_SETW);
  37.  
  38.     std::wcout << L"Находим столбец с наибольшей суммой ." << std::endl;
  39.  
  40.     auto sum = 0;
  41.     const auto index = find_max_sum_and_column_index_by_abs(matrix, n, sum);
  42.  
  43.     std::wcout << L"Индекс столбца:" << index + 1 << std::endl << L"Сумма модулей: " << sum << std::endl;
  44.  
  45.     std::wcout << L"Находим наименьший элемент в " << index + 1 << L" столбце ." << std::endl;
  46.  
  47.     const auto min_value = find_min_element_in_column(matrix, n, index);
  48.  
  49.     std::wcout << L"Минимальное значение элемента: " << min_value << std::endl;
  50.     std::wcout << L"Освобождаем память." << std::endl;
  51.  
  52.     delete_matrix(matrix, n);
  53.     system("pause");
  54. }
  55.  
  56. int find_min_element_in_column(int ** const scr_matrix, const int n, const int i)
  57. {
  58.     int index = 0;
  59.  
  60.     for (int j = 1; j < n; j++)
  61.     {
  62.         if (scr_matrix[j][i] < scr_matrix[index][i])
  63.         {
  64.             index = j;
  65.         }
  66.     }
  67.  
  68.     return scr_matrix[index][i];
  69. }
  70.  
  71. int find_max_sum_and_column_index_by_abs(int** const src_matrix, const int n, int & sum)
  72. {
  73.     auto index = -1;
  74.     auto final_sum = INT_MIN;
  75.  
  76.     for (auto i = 0; i < n; i++)
  77.     {
  78.         auto temp_sum = 0;
  79.  
  80.         for (int j = 0; j < n; ++j)
  81.         {
  82.             temp_sum += abs(src_matrix[j][i]);
  83.         }
  84.  
  85.         if (temp_sum > final_sum)
  86.         {
  87.             final_sum = temp_sum;
  88.             index = i;
  89.         }
  90.     }
  91.  
  92.     sum = final_sum;
  93.  
  94.     return index;
  95. }
  96.  
  97. int** initialize_new_matrix_with_random_values(const int n, const int m)
  98. {
  99.     const auto matrix = new int *[n];
  100.  
  101.     for (auto i = 0; i < n; i++)
  102.     {
  103.         matrix[i] = new int[m];
  104.  
  105.         for (auto j = 0; j < m; j++)
  106.         {
  107.             matrix[i][j] = rand() % CONST_ABS_ELEM_VALUE * (rand() % 2 == 1 ? 1 : -1);
  108.         }
  109.     }
  110.  
  111.     return matrix;
  112. }
  113.  
  114. void delete_matrix(int** matrix, const int n)
  115. {
  116.     for (auto i = 0; i < n; i++)
  117.     {
  118.         delete[] matrix[i];
  119.     }
  120.  
  121.     delete[] matrix;
  122. }
  123.  
  124. void init_io_threads()
  125. {
  126.     std::ios_base::sync_with_stdio(false);
  127.     std::wcout.imbue(std::locale("rus_RUS.866"));
  128. }
  129.  
  130. void output_full_matrix(int const n, int const m, int** const a, const int setw_value)
  131. {
  132.     std::wcout << L"Вывод матрицы:" << std::endl << std::setw(setw_value + 1) << L"";
  133.  
  134.     for (auto i = 0; i < m; i++)
  135.     {
  136.         std::wcout << std::setw(setw_value) << i + 1;
  137.     }
  138.  
  139.     std::wcout << std::endl;
  140.  
  141.     for (auto i = 0; i < n; i++)
  142.     {
  143.         std::wcout << std::endl << std::setw(setw_value) << i + 1 << L":";
  144.         for (auto j = 0; j < m; j++)
  145.         {
  146.             std::wcout << std::setw(setw_value) << a[i][j];
  147.         }
  148.     }
  149.  
  150.     std::wcout << std::endl << L"Конец вывода." << std::endl;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment