Advertisement
avr39ripe

2dArrRowColSum

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