Advertisement
avr39ripe

arr2dRowColSum

Apr 13th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int tabSize{ 9 };
  6.     const int rows{ 4 };
  7.     const int cols{ 5 };
  8.     int arr[rows][cols]{};
  9.     int colSum[cols]{};
  10.     int sum{ 0 };
  11.  
  12.     for (int y{ 0 }; y < rows; ++y)
  13.     {
  14.         for (int x{ 0 }; x < cols; ++x)
  15.         {
  16.             arr[y][x] = rand() % 10;
  17.             sum += arr[y][x];
  18.             std::cout << arr[y][x] << '\t';
  19.             colSum[x] += arr[y][x];
  20.         }
  21.  
  22.         std::cout << "| " << sum << '\n';
  23.         sum = 0;
  24.     }
  25.     for (int x{ 0 }; x <= cols * tabSize; ++x) { std::cout << '-'; }; std::cout << '\n';
  26.     for (int x{ 0 }; x < cols; ++x) { sum += colSum[x]; std::cout << colSum[x] << '\t'; };
  27.     std::cout << "| " << sum << '\n';
  28.  
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement