Signup/Sign In

C Program To Find Sum of each row and column in a matrix

Learn C language tutorial

An array that is of the form M*N is known as a 2-D array. It is also known as a matrix. Here, M stands for the number of rows and N stands for the number of columns.

Here, we are given a 2-D array and our task is to find the sum of each row and each column. But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.

The below is the pictorial representation of the given problem.

Input: Enter the Matrix Element

1 2 3

1 2 3

1 2 3

Output:

Sum of Row 1: 6

Sum of Row 2 : 6

Sum of Row 3 : 6

Sum of Column 0 : 3

Sum of Column 1 : 6

Sum of Column 2 : 9

Program 1: Calculate the sum of each Row and Column

In this progam, an M*N matrix is declared and the sum of each row and column is calculated directly and then displayed.

Algorithm

  1. Start
  2. Declare a 2-D array i.e., an M*N matrix.
  3. Initialize the array using two for loops.
  4. Declare two variables that will store the row and column sum.
  5. Now to calculate the row sum use a nested loop.
  6. Keep the first index of the matrix constant and increment the second index to access each element of the row.
  7. Keep on adding these elements and display the result after coming out of the inner loop.
  8. Now to calculate the column sum again using the nested loop.
  9. This time increment the first index of the matrix and keep the second index of the matrix constant to access each element of the column.
  10. Keep on adding these elements and display the result after coming out of the nested loop.
  11. Stop.

In the below program a 2-D matrix is created and initialized. Here, the row and column sum are calculated directly.

#include <stdio.h>

int main()
{
    int m,n;       //Row Column Declaration
    printf("Enter the number of rows and column\n");
    scanf("%d %d",&m,&n);    //Row Column Initialization
    int arr[m][n];   //Matrix Declaration
    printf("Enter the elements of the matrix\n");
    for(int i=0;i<m;i++)     //Matrix Initialization
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\nElements in the matrix are \n");
    for(int i=0;i<m;i++)     //Print Matrix
   {
        for(int j=0;j<n;j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    printf("\nRow Sum....\n");
    for(int i=0;i<m;i++)   
    {
        int rsum=0;
        for(int j=0;j<n;j++)
        {
            rsum=rsum+arr[i][j];
        }
        printf("\nSum of all the elements in row %d is %d\n",i,rsum);
    }
    printf("\nColumn Sum....\n");
    for(int i=0;i<m;i++)
    {
        int csum=0;
        for(int j=0;j<n;j++)
        {
            csum=csum+arr[j][i];
        }
        printf("\nSum of all the elements in column %d is %d\n",i,csum);
    }
    return 0;
}


Enter the number of rows and column 3 3
Enter the elements of the matrix 1 2 3 4 5 6 7 8 9

Elements in the matrix are
1 2 3
4 5 6
7 8 9

Row Sum....

Sum of all the elements in row 0 is 6

Sum of all the elements in row 1 is 15

Sum of all the elements in row 2 is 24

Column Sum....

Sum of all the elements in column 0 is 12

Sum of all the elements in column 1 is 15

Sum of all the elements in column 2 is 18

Program 2: Calculate the sum of each Row and Column

In this method, an M*N matrix is declared and the sum of each row and column is calculated by calling a function and the result is then displayed.

Algorithm

  1. Start
  2. Declare a 2-D array i.e., an M*N matrix.
  3. Initialize the array using two for loops.
  4. Declare two variables that will store the row and column sum.
  5. Now to calculate the row sum call a function.
  6. Keep the first index of the matrix constant and increment the second index to access each element of the row.
  7. Keep on adding these elements and display the result after coming out of the inner loop.
  8. Now to calculate the column sum call another function.
  9. This time increment the first index of the matrix and keep the second index of the matrix constant to access each element of the column.
  10. Keep on adding these elements and display the result after coming out of the nested loop.
  11. Stop.

In this program, two functions are called to calculate the sum of each row and each column.

#include<stdio.h>

void rowSum(int arr[10][10], int m, int n);
void columnSum(int arr[10][10], int m, int n);
 
int main()
{
 	int a[10][10], m,n;                     //Matrix and its size Declaration
  
 	printf("\n Please Enter Number of rows and columns  :  ");
 	scanf("%d %d", &m, &n);                  //Initialize matrix size 
 
 	printf("\n Please Enter the Matrix Elements \n");
 	for(int i = 0; i < m; i++)               //Initialize the matrix
  	{
   		for(int j= 0; j < n; j++)
    	{
      		scanf("%d", &a[i][j]);
    	}
  	}
    printf("Matrix Elements are...");
    for(int i = 0; i < m; i++)               //Print the matrix
  	{
   		for(int j= 0; j < n; j++)
    	{
      		printf("%d ",a[i][j]);
    	}
  	}
    printf("\nRow Sum...\n");
   	rowSum(a, m, n);                           //Function call for row sum
   	printf("\nColumn Sum...\n");
	columnSum(a, m, n); 	                   //Function call for column sum

 	return 0;
} 

void rowSum(int arr[10][10], int m, int n)      //Function for row sum
{
		
 	for(int i = 0; i < m; i++)
  	{
  	    int rsum=0;
  		for(int j = 0;j < n; j++)
  		{
  			rsum = rsum + arr[i][j];  			
		}
		printf("\nThe Sum of Elements of row %d is %d",i+1,rsum );
    }
}

void columnSum(int arr[10][10], int m, int n)     //Function for Column sum
{
 	for(int i = 0; i < m; i++)
  	{
  	    int csum=0;
  		for(int j = 0; j < n; j++)
  		{
  			csum = csum + arr[j][i];  			
		}
		printf("\nThe Sum of Elements of Column %d is  %d",i+1,csum );
    }
}


Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Elements : 1 2 3 4 5 6 7 8 9

Matrix Elements are...
1 2 3
4 5 6
7 8 9

Row Sum...

The Sum of Elements of row 1 is 6
The Sum of Elements of row 2 is 15
The Sum of Elements of row 3 is 24
Column Sum...

The Sum of Elements of Column 1 is 12
The Sum of Elements of Column 2 is 15
The Sum of Elements of Column 3 is 18