C Program to Find Sum Each Row and Column of Matrix

Here is the C program to calculate the sum of each row and columns of matrix. Given a matrix A of size mxn, we have to calculate the sum of elements of each rows and columns of matrix. we will use one array of length m to store sum of elements of each row and an array of length n to store sum of elements of each column.


Algorithm to find sum of each row and column of matrix
Let A be a matrix of size mxn.
  • Initialize an array rowSum of length m with zero, to store sum of elements of m rows of matrix.
  • Initialize an array colSumof length n with zero, to store sum of elements of n columns of matrix.
  • Traverse the input array row wise, from left to right.
  • Any element A[i][j] of matrix is part of ith row and jth column. Hence, we will add A[i][j] to sum of ith row and jth column.
    rowSum[i] = rowSum[i] + A[i][j], and colSum[j] = colSum[j] + A[i][j].
  • Once we traverse whole array, we will get row and column sum in rowSum and colSum array respectively.

C program to print sum of each row and column of matrix

This program first takes order of matrix as input from user and then takes elements of input matrix as input using two for loops. To calculate the sum of each row and column of matrix, it traverses the matrix row wise from left to right using two for loops(check line 22 and 23 below).

Every element inputMatrix[rowCounter][colCounter] is part of rowCounter row and colCounter column. Hence, it adds it's value to rowSum[rowCounter] and colSum[colCounter]. Finally it prints the row and column sum by iterating over rowSum and colSum array.

#include <stdio.h>

int main(){
    int rows, cols, rowCounter, colCounter;
    int inputMatrix[50][50], rowSum[50] = {0}, colSum[50] = {0};
    printf("Enter size of a matrix\n");
    scanf("%d %d", &rows, &cols);
    
    printf("Enter matrix of size %dX%d\n", rows, cols);
    /* Input matrix */
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            scanf("%d", &inputMatrix[rowCounter][colCounter]);
        }
    }
    /* Calculate sum of each row and column */
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            /* Add this element in it's row sum */
            rowSum[rowCounter] += inputMatrix[rowCounter][colCounter];
            /* Add this element in it's column sum */
            colSum[colCounter] += inputMatrix[rowCounter][colCounter];
        }
    }
    
    /* Print rows sum */
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        printf("Sum of row number %d is %d\n",
            rowCounter, rowSum[rowCounter]);
    }
    printf("\n");
    /* Print columns sum */
    for(colCounter = 0; colCounter < cols; colCounter++){
        printf("Sum of column number %d is %d\n",
            colCounter, colSum[colCounter]);
    }
    
    return 0;
}
Output
Enter size of a matrix
2 3
Enter matrix of size 2X3
1 0 1
1 1 2
Sum of row number 0 is 2
Sum of row number 1 is 4

Sum of column number 0 is 2
Sum of column number 1 is 1
Sum of column number 2 is 3 

Related Topics
C Program to find sum of diagonal elements of matrix
C program to add two matrix
C Program for subtraction of two matrix
C program to find scalar multiplication of a matrix
C program to print upper triangular matrix
C Program for matrix multiplication
C program to find a substring from a given string
C Program to print a matrix diagonally
List of all C programs