avr39-ripe

2dArrayRowColSum

Jul 11th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int rows = 5;
  6.     const int cols = 9;
  7.    
  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.     std::cout << '\n';
  26.     for (int x = 0; x < cols; x++) { std::cout << colSum[x] << '\t'; } std::cout << '\n';
  27. }
Advertisement
Add Comment
Please, Sign In to add comment