Find sum of each row and column of a matrix in C++

Hello,
In this tutorial, we will learn how to calculate the sum of each row and column of a matrix in C++ programming language.
Let’s take an example to understand it in a better way.

Example:

consider a matrix of 4x4:
input Matrix :      
                  1  5  6  2  
                  4  7  9  1   
                  0  7  5  6 
                  6  4  0  0

The sum of each Rows should be as follows:

sum of 1st row: 1 + 5 + 6 + 2 = 14
sum of 2nd row: 4 + 7 + 9 + 1 = 21
sum of 3rd row: 0 + 7 + 5 + 6 = 18
sum of 4th row: 6 + 4 + 0 + 0 = 10

The sum of each Colomns should be as follows: 

sum of 1st column: 1 + 4 + 0 + 6 = 11
sum of 2nd column: 5 + 7 + 7 + 4 = 23
sum of 3rd column: 6 + 9 + 5 + 0 = 20
sum of 4th column: 2 + 1 + 6 + 0 = 9

The approach can be implemented by the following algorithm:

Algorithm

  1. Declare and initialize a matrix of size m*n in the main function. (m = size of row, n = size column)
  2. make two functions to find the sum of each column, say columnSum and sum of each Column, say rowSum.
  3. For function columns:
    i. Declare a variable sum to store the addition of elements in a column.
    ii.  Two loops will be used to traverse the matrix, where the outer loop is for row elements and the inner loop is for the column element.
    iii. Calculate the sum for each row by adding the present element in a row.
    iv. Display the sum of that row and make sum = 0.
    v. Repeat for the remaining row.
  4. For function rowSum:
    i. Declare a variable sum to store the addition of elements in a row.
    ii.  Two loops will be used to traverse the matrix, where the outer loop is for row elements and the inner loop is for the column element.
    iii. Calculate the sum for each row by adding the present element in a column.
    iv. Display the sum of that column and make sum = 0.
    v. Repeat for the remaining column.

The implementation of the Algorithm in C++ is as follows:

Sum of each Row and Column of a matrix in C++

#include <cstdlib>
#include <iostream>

#define size 100

using namespace std;

//Our function to find and display the sum of each row
void rowSum(int arr[size][size] ,int m,int n){
    int sum;
    
    //finding column sum
    for(int i = 0; i < n; i++){
        sum = 0;
        
        for(int j = 0; j < m; j++){
            sum = sum + arr[i][j];      //adding each element of row
        }
        
        cout<<"sum of "<<"row "<<i+1<<" is: " <<sum<<endl;     
    }
}
//function to find and display sum of each column
void columnSum(int arr[size][size], int m, int n){
    int sum;
    
    //finding column sum
    for(int i = 0; i < n; i++){
        sum = 0;
        
        for(int j = 0;j < m; j++){
            sum = sum + arr[j][i];      //adding each element of column
        }
        cout<<"Addition of column "<<i + 1<<" is: "<<sum<<endl;   
    }
}

int main(int argc, char** argv) {
    int arr[size][size], m ,n;
    
    cout<<"Enter the size of row: ";
    cin>>m;
    cout<<"\nEnter the size of column: ";
    cin>>n;
    
    cout<<"\nInput the matrix: "<<endl;
    
    //initializing the matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cin>>arr[i][j];
        }
    }
    
    cout<<"\nSum of each row: \n"<<endl;
    rowSum(arr, m, n);
    
    cout<<"\nSum of each column: \n"<<endl;
    columnSum(arr, m ,n);
    
    return 0;
}

Output:

Enter the size of row: 4

Enter the size of column: 4

Input the matrix: 
1 5 6 2
4 7 9 1
0 7 5 6
6 4 0 0

Sum of each row: 

sum of row 1 is: 14
sum of row 2 is: 21
sum of row 3 is: 18
sum of row 4 is: 10

Sum of each column: 

Addition of column 1 is: 11
Addition of column 2 is: 23
Addition of column 3 is: 20
Addition of column 4 is: 9

Time Complexity: O(m*n)

Note: In the program, the maximum size of the matrix is restricted to 100 only as the matrix size is initialized with “matrix [size] [size] ” and here the size = 100 from “#define size 100”.




You may also learn:

  1. Addition of two matrices in C++
  2. Print the corner elements of the matrix using C++


Leave a Reply

Your email address will not be published. Required fields are marked *