Advertisement
Guest User

Сумма и произведение элементов столбца в матрице.

a guest
Nov 4th, 2017
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 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** get_multiplication_and_sum_of_lines(int** src_matrix, const int n, const int m);
  14.  
  15. int main()
  16. {
  17.     srand(NULL);
  18.     init_io_threads();
  19.  
  20.     auto n = 12;
  21.     auto m = 14;
  22.  
  23.     std::wcout << L"Введите кол-во строк: ";
  24.     std::cin >> n;
  25.     std::wcout << L"Введите кол-во столбцов: ";
  26.     std::cin >> m;
  27.     std::wcout << std::endl << L"Введена матрица " << n << L" x " << m << L"." << std::endl;
  28.     std::wcout << L"Матрица заполняется случайными значениями." << std::endl;
  29.  
  30.     const auto matrix = initialize_new_matrix_with_random_values(n, m);
  31.  
  32.     output_full_matrix(n, m, matrix, DEFAULT_VALUE_SETW);
  33.  
  34.     std::wcout << L"Находим произведение и сумму элементов каждого столбца в матрице." << std::endl;
  35.  
  36.     const auto result = get_multiplication_and_sum_of_lines(matrix, n, m);
  37.  
  38.     std::wcout << L"Выводим полученные значение (1-ый столбец - сумма, 2-ой - произведение)." << std::endl;
  39.  
  40.     output_full_matrix(m, 2, result, DEFAULT_VALUE_SETW * 5);
  41.  
  42.     std::wcout << L"Освобождаем память." << std::endl;
  43.  
  44.     delete_matrix(matrix, n);
  45.     delete_matrix(result, m);
  46.  
  47.     system("pause");
  48. }
  49.  
  50. int** get_multiplication_and_sum_of_lines(int** src_matrix, const int n, const int m)
  51. {
  52.     const auto result = new int *[m];
  53.  
  54.     for (auto i = 0; i < m; i++)
  55.     {
  56.         result[i] = new int[2];
  57.  
  58.         for (auto j = 0; j < 2; j++)
  59.         {
  60.             result[i][j] = j;
  61.         }
  62.  
  63.         for (auto j = 0; j < n; j++)
  64.         {
  65.             result[i][0] += src_matrix[j][i];
  66.             result[i][1] *= src_matrix[j][i];
  67.         }
  68.     }
  69.  
  70.     return result;
  71. }
  72.  
  73. int** initialize_new_matrix_with_random_values(const int n, const int m)
  74. {
  75.     const auto matrix = new int *[n];
  76.  
  77.     for (auto i = 0; i < n; i++)
  78.     {
  79.         matrix[i] = new int[m];
  80.  
  81.         for (auto j = 0; j < m; j++)
  82.         {
  83.             matrix[i][j] = rand() % CONST_ABS_ELEM_VALUE * (rand() % 2 == 1 ? 1 : -1);
  84.         }
  85.     }
  86.  
  87.     return matrix;
  88. }
  89.  
  90. void delete_matrix(int** matrix, const int n)
  91. {
  92.     for (auto i = 0; i < n; i++)
  93.     {
  94.         delete[] matrix[i];
  95.     }
  96.  
  97.     delete[] matrix;
  98. }
  99.  
  100. void init_io_threads()
  101. {
  102.     std::ios_base::sync_with_stdio(false);
  103.     std::wcout.imbue(std::locale("rus_RUS.866"));
  104. }
  105.  
  106. void output_full_matrix(int const n, int const m, int** const a, const int setw_value)
  107. {
  108.     std::wcout << L"Вывод матрицы:" << std::endl << std::setw(setw_value + 1) << L"";
  109.  
  110.     for (auto i = 0; i < m; i++)
  111.     {
  112.         std::wcout << std::setw(setw_value) << i + 1;
  113.     }
  114.  
  115.     std::wcout << std::endl;
  116.  
  117.     for (auto i = 0; i < n; i++)
  118.     {
  119.         std::wcout << std::endl << std::setw(setw_value) << i + 1 << L":";
  120.         for (auto j = 0; j < m; j++)
  121.         {
  122.             std::wcout << std::setw(setw_value) << a[i][j];
  123.         }
  124.     }
  125.  
  126.     std::wcout << std::endl << L"Конец вывода." << std::endl;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement