Advertisement
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** get_multiplication_and_sum_of_lines(int** src_matrix, const int n, const int m);
- int main()
- {
- srand(NULL);
- init_io_threads();
- auto n = 12;
- auto m = 14;
- std::wcout << L"Введите кол-во строк: ";
- std::cin >> n;
- std::wcout << L"Введите кол-во столбцов: ";
- std::cin >> m;
- std::wcout << std::endl << L"Введена матрица " << n << L" x " << m << L"." << std::endl;
- std::wcout << L"Матрица заполняется случайными значениями." << std::endl;
- const auto matrix = initialize_new_matrix_with_random_values(n, m);
- output_full_matrix(n, m, matrix, DEFAULT_VALUE_SETW);
- std::wcout << L"Находим произведение и сумму элементов каждого столбца в матрице." << std::endl;
- const auto result = get_multiplication_and_sum_of_lines(matrix, n, m);
- std::wcout << L"Выводим полученные значение (1-ый столбец - сумма, 2-ой - произведение)." << std::endl;
- output_full_matrix(m, 2, result, DEFAULT_VALUE_SETW * 5);
- std::wcout << L"Освобождаем память." << std::endl;
- delete_matrix(matrix, n);
- delete_matrix(result, m);
- system("pause");
- }
- int** get_multiplication_and_sum_of_lines(int** src_matrix, const int n, const int m)
- {
- const auto result = new int *[m];
- for (auto i = 0; i < m; i++)
- {
- result[i] = new int[2];
- for (auto j = 0; j < 2; j++)
- {
- result[i][j] = j;
- }
- for (auto j = 0; j < n; j++)
- {
- result[i][0] += src_matrix[j][i];
- result[i][1] *= src_matrix[j][i];
- }
- }
- return result;
- }
- 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
Advertisement