polygraph

Сумма всех положительных элементов матрицы по столбцам

May 18th, 2021
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3.  
  4. int** createMatrix(int N);    
  5. void fullMatrix(int** M, int N);  
  6. void printMatrix(int** M, int N);
  7. void printSumPlusColumn(int** M, int N);
  8.  
  9. int main()
  10. {
  11.     setlocale(0, "");
  12.     /*const int N = 6;*/
  13.     int N = 0;
  14.     std::cout << "Введи размер матрицы:)" << std::endl;
  15.     std::cin >> N;
  16.     int** A = createMatrix(N);
  17.     fullMatrix(A, N);
  18.     printMatrix(A, N);
  19.     printSumPlusColumn(A, N);
  20.     delete[] A;
  21.     system("pause");
  22.     return 0;
  23. }
  24.  
  25. int** createMatrix(const int N){
  26.     int** M = new int* [N];
  27.     for (int i = 0; i < N; i++)
  28.     {
  29.         M[i] = new int[N];
  30.     }
  31.     return M;
  32. }
  33.  
  34. void fullMatrix(int** M, const int N){
  35.     for (int i = 0; i < N; i++)
  36.     {
  37.         for (int j = 0; j < N; j++)
  38.         {
  39.             M[i][j] = -5+rand() % 10;
  40.         }
  41.  
  42.     }
  43. }
  44.  
  45. void printMatrix(int** M, const int N){
  46.     std::cout << "Матрица:" << std::endl;
  47.     std::cout << "-----------------------" << std::endl;
  48.     for (int i = 0; i < N; i++)
  49.     {
  50.         for (int j = 0; j < N; j++)
  51.         {
  52.             std::cout << M[i][j] << '\t';
  53.         }
  54.         std::cout << std::endl;
  55.     }
  56. }
  57.  
  58. void printSumPlusColumn(int **matrix, const int N){
  59.     std::cout << "Сумма положительных чисел в каждом столбце:" << std::endl;
  60.     std::cout << "-----------------------" << std::endl;
  61.     int sum;
  62.     for (int i = 0; i < N; ++i)
  63.     {
  64.         sum = 0;
  65.         for (int j = 0; j < N; ++j) {
  66.             if (matrix[j][i] > 0)
  67.                sum += matrix[j][i];
  68.         }
  69.         std::cout << sum << '\t';
  70.     }
  71.     std::cout << std::endl << "-----------------------" << std::endl;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment