Program to find the Sum of each Row and each Column of a Matrix
Given a matrix of order m×n, the task is to find out the sum of each row and each column of a matrix.
Examples:
Input: array[4][4] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
Output: Sum of the 0 row is = 4
Sum of the 1 row is = 8
Sum of the 2 row is = 12
Sum of the 3 row is = 16
Sum of the 0 column is = 10
Sum of the 1 column is = 10
Sum of the 2 column is = 10
Sum of the 3 column is = 10
Approach:
The sum of each row and each column can be calculated by traversing through the matrix and adding up the elements.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define m 4
#define n 4
void row_sum( int arr[m][n])
{
int i,j,sum = 0;
cout << "\nFinding Sum of each row:\n\n" ;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[i][j];
}
cout
<< "Sum of the row "
<< i << " = " << sum
<< endl;
sum = 0;
}
}
void column_sum( int arr[m][n])
{
int i,j,sum = 0;
cout << "\nFinding Sum of each column:\n\n" ;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[j][i];
}
cout
<< "Sum of the column "
<< i << " = " << sum
<< endl;
sum = 0;
}
}
int main()
{
int i,j;
int arr[m][n];
int x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = x++;
row_sum(arr);
column_sum(arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int m = 4 ;
static int n = 4 ;
static void row_sum( int arr[][])
{
int i, j, sum = 0 ;
System.out.print( "\nFinding Sum of each row:\n\n" );
for (i = 0 ; i < m; ++i) {
for (j = 0 ; j < n; ++j) {
sum = sum + arr[i][j];
}
System.out.println( "Sum of the row " + i + " = "
+ sum);
sum = 0 ;
}
}
static void column_sum( int arr[][])
{
int i, j, sum = 0 ;
System.out.print(
"\nFinding Sum of each column:\n\n" );
for (i = 0 ; i < m; ++i) {
for (j = 0 ; j < n; ++j) {
sum = sum + arr[j][i];
}
System.out.println( "Sum of the column " + i
+ " = " + sum);
sum = 0 ;
}
}
public static void main(String[] args)
{
int i, j;
int [][] arr = new int [m][n];
int x = 1 ;
for (i = 0 ; i < m; i++)
for (j = 0 ; j < n; j++)
arr[i][j] = x++;
row_sum(arr);
column_sum(arr);
}
}
|
Python 3
import numpy as np
m , n = 4 , 4
def row_sum(arr) :
sum = 0
print ( "\nFinding Sum of each row:\n" )
for i in range (m) :
for j in range (n) :
sum + = arr[i][j]
print ( "Sum of the row" ,i, "=" , sum )
sum = 0
def column_sum(arr) :
sum = 0
print ( "\nFinding Sum of each column:\n" )
for i in range (m) :
for j in range (n) :
sum + = arr[j][i]
print ( "Sum of the column" ,i, "=" , sum )
sum = 0
if __name__ = = "__main__" :
arr = np.zeros(( 4 , 4 ))
x = 1
for i in range (m) :
for j in range (n) :
arr[i][j] = x
x + = 1
row_sum(arr)
column_sum(arr)
|
C#
using System;
class GFG {
static int m = 4;
static int n = 4;
static void row_sum( int [, ] arr)
{
int i, j, sum = 0;
Console.Write( "\nFinding Sum of each row:\n\n" );
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[i, j];
}
Console.WriteLine( "Sum of the row " + i + " = "
+ sum);
sum = 0;
}
}
static void column_sum( int [, ] arr)
{
int i, j, sum = 0;
Console.Write( "\nFinding Sum of each"
+ " column:\n\n" );
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[j, i];
}
Console.WriteLine( "Sum of the column " + i
+ " = " + sum);
sum = 0;
}
}
public static void Main()
{
int i, j;
int [, ] arr = new int [m, n];
int x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i, j] = x++;
row_sum(arr);
column_sum(arr);
}
}
|
PHP
<?php
$m = 4;
$n = 4;
function row_sum(& $arr )
{
$sum = 0;
echo "Finding Sum of each row:\n\n" ;
for ( $i = 0; $i < m; ++ $i )
{
for ( $j = 0; $j < n; ++ $j )
{
$sum = $sum + $arr [ $i ][ $j ];
}
echo "Sum of the row " . $i .
" = " . $sum . "\n" ;
$sum = 0;
}
}
function column_sum(& $arr )
{
$sum = 0;
echo "\nFinding Sum of each column:\n\n" ;
for ( $i = 0; $i < m; ++ $i )
{
for ( $j = 0; $j < n; ++ $j )
{
$sum = $sum + $arr [ $j ][ $i ];
}
echo "Sum of the column " . $i .
" = " . $sum . "\n" ;
$sum = 0;
}
}
$arr = array_fill (0, $m , array_fill (0, $n , NULL));
$x = 1;
for ( $i = 0; $i < $m ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$arr [ $i ][ $j ] = $x ++;
row_sum( $arr );
column_sum( $arr );
?>
|
Javascript
<script>
var m= 4;
var n= 4;
function row_sum( arr)
{
var i,j,sum = 0;
document.write( "<br>" + "\nFinding Sum of each row:" + "<br>" );
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[i][j];
}
document.write( "Sum of the row "
+ i + " = " + sum
+ "<br>" );
sum = 0;
}
}
function column_sum(arr)
{
var i,j,sum = 0;
document.write( "<br>" + "Finding Sum of each column:" + "<br>" );
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[j][i];
}
document.write( "Sum of the column "
+ i + " = " + sum
+ "<br>" );
sum = 0;
}
}
var i,j;
var arr= new Array(m).fill(0);
for ( var k=0;k<m;k++)
{
arr[k]= new Array(n).fill(0);
}
var x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j]= x++;
row_sum(arr);
column_sum(arr);
</script>
|
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
Complexity Analysis:
- Time Complexity: O(N*M), as we are using nested loops for traversing the matrix.
- Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
06 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...