This is a C program to calculate the sum of the elements of each row & column in a given matrix.

This C program reads a matrix A (MxN) & finds the following using functions

a) Sum of the elements of each row

b) Sum of the elements of each column

c) Find the sum of all the elements of the matrix

1. Take the MxN matrix as input.

2. Define two functions separately for row sum and column sum.

3. Use for loops to calculate the sum of the elements of each row & column in a given matrix.

4. Either add all the calculated row sum or column sum to get the sum of all elements of the matrix.

Here is source code of the C program to calculate the sum of the elements of each row & column. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C program to read a matrix A (MxN) & find the following using`

`* functions a) Sum of the elements of each row`

`* b) Sum of the elements of each column`

`* c) Find the sum of all the elements of the matrix`

`* Output the computed results`

`*/`

`#include <stdio.h>`

int Addrow(int array1[10][10], int k, int c);

int Addcol(int array1[10][10], int k, int r);

void main()

`{`

int arr[10][10];

int i, j, row, col, rowsum, colsum, sumall=0;

printf("Enter the order of the matrix \n");

scanf("%d %d", &row, &col);

printf("Enter the elements of the matrix \n");

for (i = 0; i < row; i++)

`{`

for (j = 0; j < col; j++)

`{`

scanf("%d", &arr[i][j]);

`}`

`}`

printf("Input matrix is \n");

for (i = 0; i < row; i++)

`{`

for (j = 0; j < col; j++)

`{`

printf("%3d", arr[i][j]);

`}`

printf("\n");

`}`

`/* computing row sum */`

for (i = 0; i < row; i++)

`{`

rowsum = Addrow(arr, i, col);

printf("Sum of row %d = %d\n", i + 1, rowsum);

`}`

`/* computing col sum */`

for (j = 0; j < col; j++)

`{`

colsum = Addcol(arr, j, row);

printf("Sum of column %d = %d\n", j + 1, colsum);

`}`

`/* computation of all elements */`

for (j = 0; j < row; j++)

`{`

sumall = sumall + Addrow(arr, j, col);

`}`

printf("Sum of all elements of matrix = %d\n", sumall);

`}`

`/* Function to add each row */`

int Addrow(int array1[10][10], int k, int c)

`{`

int rsum = 0, i;

for (i = 0; i < c; i++)

`{`

rsum = rsum + array1[k][i];

`}`

return(rsum);

`}`

`/* Function to add each column */`

int Addcol(int array1[10][10], int k, int r)

`{`

int csum = 0, j;

for (j = 0; j < r; j++)

`{`

csum = csum + array1[j][k];

`}`

return(csum);

`}`

1. Take M & N of a MxN matrix as input and store it in the variables row & column respectively.

2. Take all the elements of the matrix using two for loops and store in the array a[][].

3. Define two functions namely Addrow and Addcol for calculating row sum and column sum respectively.

4. Call each function inside a for loop.

5. Use another for loop inside each function to calculate their respective sum.

6. Use variables rsum & csum to store row sum and column sum respectively.

7. Print the output using variables rsum and csum.

8. For the sum of all elements of the matrix call either function Addrow or Addcol inside a for loop.

9. Firstly initialize the variable sumall to zero, later increment it with the returned values from the called function.

10. Print the variable sumall and exit.

Enter the order of the matrix 3 3 Enter the elements of the matrix 2 3 4 7 1 5 3 8 9 Input matrix is 2 3 4 7 1 5 3 8 9 Sum of row 1 = 9 Sum of row 2 = 13 Sum of row 3 = 20 Sum of column 1 = 12 Sum of column 2 = 12 Sum of column 3 = 18 Sum of all elements of matrix = 42

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms