Row and Column Sums

Lab C++ Beginner 10 points

Read an r×c matrix into a vector<vector<int>>. Print the row sums on one line and the column sums on the next.

Input

r c, then r rows.

Output

Two lines.

Example

Input

2 3
1 2 3
4 5 6

Output

6 15
5 7 9
Sample input available Sample output available
Editor
C++

Hints

vector<vector<int>> m(r, vector<int>(c)) creates an r x c grid of zeros.

Solution explanation

Range-for with references fills the grid without index variables.

Show solution
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int r, c;
    cin >> r >> c;
    vector<vector<int>> m(r, vector<int>(c));
    for (auto &row : m)
        for (int &x : row) cin >> x;
    for (int i = 0; i < r; i++) {
        int s = 0;
        for (int x : m[i]) s += x;
        cout << s << (i < r - 1 ? " " : "\n");
    }
    for (int j = 0; j < c; j++) {
        int s = 0;
        for (int i = 0; i < r; i++) s += m[i][j];
        cout << s << (j < c - 1 ? " " : "\n");
    }
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD