Open In App

Program to find the Sum of each Row and each Column of a Matrix

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a matrix mat of size m × n, the task is to compute the sum of each row and each column of the matrix.

Examples:

Input: mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ]
Output: Sum of row 0 = 10
Sum of row 1 = 26
Sum of row 2 = 42
Sum of row 3 = 58
Sum of column 0 = 28
Sum of column 1 = 32
Sum of column 2 = 36
Sum of column 3 = 40

Input: mat= [ [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4] ]
Output: Sum of row 0 = 4
Sum of row 1 = 8
Sum of row 2 = 12
Sum of row 3 = 16
Sum of column 0 = 10
Sum of column 1 = 10
Sum of column 2 = 10
Sum of column 3 = 10

Approach:  

The idea is to compute the sum of each row and each column separately. First, iterate through each row, summing up its elements and storing the result. Then, iterate through each column, summing up its elements and storing the result. This ensures that all row sums are calculated before moving to column sums. The final result contains row sums followed by column sums.

C++
// C++ program to find the sum
// of each row and column of a matrix
#include <bits/stdc++.h>
using namespace std;

vector<int> sumRowsAndCols(vector<vector<int>> &mat) {
    int m = mat.size(), n = mat[0].size();
    vector<int> result;

    // Compute row sums
    for(int i = 0; i < m; i++) {
        int sum = 0;
        for(int j = 0; j < n; j++) {
            sum += mat[i][j];
        }
        result.push_back(sum);
    }

    // Compute column sums
    for(int j = 0; j < n; j++) {
        int sum = 0;
        for(int i = 0; i < m; i++) {
            sum += mat[i][j];
        }
        result.push_back(sum);
    }

    return result;
}

// Function to print the result vector
void printArray(vector<int> &arr, int m, int n) {
    for(int i = 0; i < m; i++) {
        cout << "Sum of row " << i << " = " << arr[i] << endl;
    }
    cout << endl;
    for(int j = 0; j < n; j++) {
        cout << "Sum of column " << j << " = " << arr[m + j] << endl;
    }
}

int main() {
    vector<vector<int>> mat = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };

    vector<int> result = sumRowsAndCols(mat);
    printArray(result, 4, 4);

    return 0;
}
Java
// Java program to find the sum
// of each row and column of a matrix

class GfG {
    
    // Function to compute row and column sums
    static int[] sumRowsAndCols(int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int[] result = new int[m + n];

        // Compute row sums
        for(int i = 0; i < m; i++) {
            int sum = 0;
            for(int j = 0; j < n; j++) {
                sum += mat[i][j];
            }
            result[i] = sum;
        }

        // Compute column sums
        for(int j = 0; j < n; j++) {
            int sum = 0;
            for(int i = 0; i < m; i++) {
                sum += mat[i][j];
            }
            result[m + j] = sum;
        }

        return result;
    }

    // Function to print the result array
    static void printArray(int[] arr, int m, int n) {
        for(int i = 0; i < m; i++) {
            System.out.println("Sum of row " + i + " = " + arr[i]);
        }
        System.out.println();
        for(int j = 0; j < n; j++) {
            System.out.println("Sum of column " + j + " = " + arr[m + j]);
        }
    }

    public static void main(String[] args) {
        int[][] mat = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
        };

        int[] result = sumRowsAndCols(mat);
        printArray(result, 4, 4);
    }
}
Python
# Python program to find the sum
# of each row and column of a matrix

def sumRowsAndCols(mat):
    m, n = len(mat), len(mat[0])
    result = []

    # Compute row sums
    for i in range(m):
        sum = 0
        for j in range(n):
            sum += mat[i][j]
        result.append(sum)

    # Compute column sums
    for j in range(n):
        sum = 0
        for i in range(m):
            sum += mat[i][j]
        result.append(sum)

    return result

# Function to print the result list
def printArray(arr, m, n):
    for i in range(m):
        print("Sum of row", i, "=", arr[i])
    print()
    for j in range(n):
        print("Sum of column", j, "=", arr[m + j])

if __name__ == "__main__":
    mat = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 16]
    ]

    result = sumRowsAndCols(mat)
    printArray(result, 4, 4)
C#
// C# program to find the sum
// of each row and column of a matrix
using System;

class GfG {
    // Function to compute row and column sums
    static int[] SumRowsAndCols(int[,] mat) {
        int m = mat.GetLength(0), n = mat.GetLength(1);
        int[] result = new int[m + n];

        // Compute row sums
        for(int i = 0; i < m; i++) {
            int sum = 0;
            for(int j = 0; j < n; j++) {
                sum += mat[i, j];
            }
            result[i] = sum;
        }

        // Compute column sums
        for(int j = 0; j < n; j++) {
            int sum = 0;
            for(int i = 0; i < m; i++) {
                sum += mat[i, j];
            }
            result[m + j] = sum;
        }

        return result;
    }

    // Function to print the result array
    static void PrintArray(int[] arr, int m, int n) {
        for(int i = 0; i < m; i++) {
            Console.WriteLine("Sum of row " + i + " = " + arr[i]);
        }
        Console.WriteLine();
        for(int j = 0; j < n; j++) {
            Console.WriteLine("Sum of column " + j + " = " + arr[m + j]);
        }
    }

    public static void Main() {
        int[,] mat = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
        };

        int[] result = SumRowsAndCols(mat);
        PrintArray(result, 4, 4);
    }
}
JavaScript
// Javascript program to find the sum
// of each row and column of a matrix

function sumRowsAndCols(mat) {
    let m = mat.length, n = mat[0].length;
    let result = [];

    // Compute row sums
    for (let i = 0; i < m; i++) {
        let sum = 0;
        for (let j = 0; j < n; j++) {
            sum += mat[i][j];
        }
        result.push(sum);
    }

    // Compute column sums
    for (let j = 0; j < n; j++) {
        let sum = 0;
        for (let i = 0; i < m; i++) {
            sum += mat[i][j];
        }
        result.push(sum);
    }

    return result;
}

// Function to print the result array
function printArray(arr, m, n) {
    for (let i = 0; i < m; i++) {
        console.log("Sum of row", i, "=", arr[i]);
    }
    console.log();
    for (let j = 0; j < n; j++) {
        console.log("Sum of column", j, "=", arr[m + j]);
    }
}

// Driver Code
let mat = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
];

let result = sumRowsAndCols(mat);
printArray(result, 4, 4);

Output
Finding Sum of each row:

Sum of the row 0 = 10
Sum of the row 1 = 26
Sum of the row 2 = 42
Sum of the row 3 = 58

Finding Sum of each column:

Sum of the column 0 = 28
Sum of the column 1 = 32
Sum of the column 2 = 36
Sum of the column 3 = 40

Time Complexity: O(m * n), as we traverse the entire matrix twice (once for rows and once for columns).
Auxiliary Space: O(m + n), as we store the sum of each row and each column in the result array.



Next Article
Practice Tags :

Similar Reads