TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find The Sum of Each Row And Each Column of a Matrix

Program to Find The Sum of Each Row And Each Column of a Matrix on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to find the sum of each row and each column of a matrix

Explanation

In this program, we need to calculate the sum of elements in each row and each column of the given matrix.

Program to find the sum of each row and each column of a matrix

Above diagram shows the sum of elements of each row and each column of a matrix.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the array a and store it in variables rows and cols respectively.
  3. Maintain two variables sumRow and sumCol to store the sum of elements in the specific row and the sum of elements in specific column respectively.
  4. To calculate the sum of elements in each row:
    1. Two loops will be used to traverse the array where the outer loop selects a row, and the inner loop represents the columns present in the matrix a.
    2. Calculate the sum by adding elements present in a row.
    3. Display sumRow.
    4. Repeat this for each row.
  5. To calculate the sum of elements in each column:
    1. Two loops will be used to traverse the array where the outer loop select a column, and the inner loop represents the rows present in the matrix a.
    2. Calculate the sum by adding elements present in a column.
    3. Display sumCol.
    4. Repeat this for each column.

Solution

Python

#Initialize matrix a
a = [  
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ];
 
#Calculates number of rows and columns present in given matrix
rows = len(a);
cols = len(a[0]);
 
#Calculates sum of each row of given matrix
for i in range(0, rows):
    sumRow = 0;
    for j in range(0, cols):
        sumRow = sumRow + a[i][j];
    print("Sum of " + str(i+1) +" row: " + str(sumRow));
 
#Calculates sum of each column of given matrix
for i in range(0, rows):
    sumCol = 0;
    for j in range(0, cols):
        sumCol = sumCol + a[j][i];
    print("Sum of " + str(i+1) +" column: " + str(sumCol));

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

C

#include <stdio.h>
 
int main()
{
    int rows, cols, sumRow, sumCol;

    
    //Initialize matrix a
    int a[][3] = {   
                    {1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}
                };
    
    //Calculates number of rows and columns present in given matrix
    rows = (sizeof(a)/sizeof(a[0]));
    cols = (sizeof(a)/sizeof(a[0][0]))/rows;
    
    //Calculates sum of each row of given matrix
    for(int i = 0; i < rows; i++){
        sumRow = 0;
        for(int j = 0; j < cols; j++){
          sumRow = sumRow + a[i][j];
        }
        printf("Sum of %d row: %d\n", (i+1), sumRow);
    }
    
    //Calculates sum of each column of given matrix
    for(int i = 0; i < cols; i++){
        sumCol = 0;
        for(int j = 0; j < rows; j++){
          sumCol = sumCol + a[j][i];
        }
        printf("Sum of %d column: %d\n", (i+1), sumCol);
    }
        
    return 0;
}

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

JAVA

public class SumofRowColumn
{
    public static void main(String[] args) {
        int rows, cols, sumRow, sumCol;
        
        //Initialize matrix a
        int a[][] = {   
                        {1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}
                    };
          
          //Calculates number of rows and columns present in given matrix
          rows = a.length;
        cols = a[0].length;
        
        //Calculates sum of each row of given matrix
        for(int i = 0; i < rows; i++){
            sumRow = 0;
            for(int j = 0; j < cols; j++){
              sumRow = sumRow + a[i][j];
            }
            System.out.println("Sum of " + (i+1) +" row: " + sumRow);
        }
        
        //Calculates sum of each column of given matrix
        for(int i = 0; i < cols; i++){
            sumCol = 0;
            for(int j = 0; j < rows; j++){
              sumCol = sumCol + a[j][i];
            }
            System.out.println("Sum of " + (i+1) +" column: " + sumCol);
        }
    }
}

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

C#

using System;
                    
public class SumofRowColumn
{
    public static void Main()
    {
        int rows, cols, sumRow, sumCol;
        
        //Initialize matrix a
        int[,] a = {   
                        {1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}
                   };
          
          //Calculates number of rows and columns present in given matrix
          rows = a.GetLength(0);
        cols = a.GetLength(1);
        
        //Calculates sum of each row of given matrix
        for(int i = 0; i < rows; i++){
            sumRow = 0;
            for(int j = 0; j < cols; j++){
              sumRow = sumRow + a[i,j];
            }
            Console.WriteLine("Sum of " + (i+1) +" row: " + sumRow);
        }
        
        //Calculates sum of each column of given matrix
        for(int i = 0; i < cols; i++){
            sumCol = 0;
            for(int j = 0; j < rows; j++){
              sumCol = sumCol + a[j,i];
            }
            Console.WriteLine("Sum of " + (i+1) +" column: " + sumCol);
        }
    }
}

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize matrix a
$a = array(   
             array(1, 2, 3),
             array(4, 5, 6),
             array(7, 8, 9)
           );
 
//Calculates number of rows and columns present in given matrix
$rows = count($a);
$cols = count($a[0]);
 
//Calculates sum of each row of given matrix
for($i = 0; $i < $rows; $i++){
    $sumRow = 0;
    for($j = 0; $j < $cols; $j++){
      $sumRow = $sumRow + $a[$i][$j];
    }
    print("Sum of " . ($i+1) ." row: " . $sumRow);
    print("<br>");
}
 
//Calculates sum of each column of given matrix
for($i = 0; $i < $cols; $i++){
    $sumCol = 0;
    for($j = 0; $j < $rows; $j++){
      $sumCol = $sumCol + $a[$j][$i];
    }
    print("Sum of " . ($i+1) . " column: " . $sumCol);
    print("<br>");
}
?>
</body>
</html>

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf