Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cstdlib"
- #include "locale"
- #include "iostream"
- #include "iomanip"
- #define CONST_ABS_ELEM_VALUE 20
- #define DEFAULT_VALUE_SETW 5
- int** initialize_new_matrix_with_random_values(int n, int m);
- void delete_matrix(int** matrix, int m);
- void output_full_matrix(int const n, int const m, int** const a, int setw_value);
- void init_io_threads();
- int find_max_sum_and_column_index_by_abs(int** const src_matrix, const int n, int & sum);
- int find_min_element_in_column(int ** const scr_matrix, const int n, const int i);
- void find_min_elem_of_max_sum_column();
- int main()
- {
- find_min_elem_of_max_sum_column();
- }
- void find_min_elem_of_max_sum_column()
- {
- srand(NULL);
- init_io_threads();
- auto n = 12;
- std::wcout << L"Введите размерность матрицы: ";
- std::cin >> n;
- std::wcout << std::endl << L"Введена матрица " << n << L" x " << n << L"." << std::endl;
- std::wcout << L"Матрица заполняется случайными значениями." << std::endl;
- const auto matrix = initialize_new_matrix_with_random_values(n, n);
- output_full_matrix(n, n, matrix, DEFAULT_VALUE_SETW);
- std::wcout << L"Находим столбец с наибольшей суммой ." << std::endl;
- auto sum = 0;
- const auto index = find_max_sum_and_column_index_by_abs(matrix, n, sum);
- std::wcout << L"Индекс столбца:" << index + 1 << std::endl << L"Сумма модулей: " << sum << std::endl;
- std::wcout << L"Находим наименьший элемент в " << index + 1 << L" столбце ." << std::endl;
- const auto min_value = find_min_element_in_column(matrix, n, index);
- std::wcout << L"Минимальное значение элемента: " << min_value << std::endl;
- std::wcout << L"Освобождаем память." << std::endl;
- delete_matrix(matrix, n);
- system("pause");
- }
- int find_min_element_in_column(int ** const scr_matrix, const int n, const int i)
- {
- int index = 0;
- for (int j = 1; j < n; j++)
- {
- if (scr_matrix[j][i] < scr_matrix[index][i])
- {
- index = j;
- }
- }
- return scr_matrix[index][i];
- }
- int find_max_sum_and_column_index_by_abs(int** const src_matrix, const int n, int & sum)
- {
- auto index = -1;
- auto final_sum = INT_MIN;
- for (auto i = 0; i < n; i++)
- {
- auto temp_sum = 0;
- for (int j = 0; j < n; ++j)
- {
- temp_sum += abs(src_matrix[j][i]);
- }
- if (temp_sum > final_sum)
- {
- final_sum = temp_sum;
- index = i;
- }
- }
- sum = final_sum;
- return index;
- }
- int** initialize_new_matrix_with_random_values(const int n, const int m)
- {
- const auto matrix = new int *[n];
- for (auto i = 0; i < n; i++)
- {
- matrix[i] = new int[m];
- for (auto j = 0; j < m; j++)
- {
- matrix[i][j] = rand() % CONST_ABS_ELEM_VALUE * (rand() % 2 == 1 ? 1 : -1);
- }
- }
- return matrix;
- }
- void delete_matrix(int** matrix, const int n)
- {
- for (auto i = 0; i < n; i++)
- {
- delete[] matrix[i];
- }
- delete[] matrix;
- }
- void init_io_threads()
- {
- std::ios_base::sync_with_stdio(false);
- std::wcout.imbue(std::locale("rus_RUS.866"));
- }
- void output_full_matrix(int const n, int const m, int** const a, const int setw_value)
- {
- std::wcout << L"Вывод матрицы:" << std::endl << std::setw(setw_value + 1) << L"";
- for (auto i = 0; i < m; i++)
- {
- std::wcout << std::setw(setw_value) << i + 1;
- }
- std::wcout << std::endl;
- for (auto i = 0; i < n; i++)
- {
- std::wcout << std::endl << std::setw(setw_value) << i + 1 << L":";
- for (auto j = 0; j < m; j++)
- {
- std::wcout << std::setw(setw_value) << a[i][j];
- }
- }
- std::wcout << std::endl << L"Конец вывода." << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment