C Program to Find the Sum of Each Row and Column of a Matrix

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

Problem Description

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

Problem Solution

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.

Program/Source Code

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.

  1. /*
  2.  * C program to read a matrix A (MxN) & find the following using
  3.  * functions a) Sum of the elements of each row
  4.  * b) Sum of the elements of each column
  5.  * c) Find the sum of all the elements of the matrix
  6.  * Output the computed results
  7.  */
  8. #include <stdio.h>
  9. int Addrow(int array1[10][10], int k, int c);
  10. int Addcol(int array1[10][10], int k, int r);
  11.  
  12. void main()
  13. {
  14.     int arr[10][10];
  15.     int i, j, row, col, rowsum, colsum, sumall=0;
  16.  
  17.     printf("Enter the order of the matrix \n");
  18.     scanf("%d %d", &row, &col);
  19.     printf("Enter the elements of the matrix \n");
  20.     for (i = 0; i < row; i++)
  21.     {
  22.         for (j = 0; j < col; j++)
  23.         {
  24.             scanf("%d", &arr[i][j]);
  25.         }
  26.     }
  27.     printf("Input matrix is \n");
  28.     for (i = 0; i < row; i++)
  29.     {
  30.         for (j = 0; j < col; j++)
  31.         {
  32.             printf("%3d", arr[i][j]);
  33.         }
  34.         printf("\n");
  35.     }
  36.     /*  computing row sum */
  37.     for (i = 0; i < row; i++)
  38.     {
  39.         rowsum = Addrow(arr, i, col);
  40.         printf("Sum of row %d = %d\n", i + 1, rowsum);
  41.     }
  42.     /*  computing col sum */
  43.     for (j = 0; j < col; j++)
  44.     {
  45.         colsum = Addcol(arr, j, row);
  46.         printf("Sum of column  %d = %d\n", j + 1, colsum);
  47.     }
  48.     /*  computation of all elements */
  49.     for (j = 0; j < row; j++)
  50.     {
  51.         sumall = sumall + Addrow(arr, j, col);
  52.     }
  53.     printf("Sum of all elements of matrix = %d\n", sumall);
  54. }
  55. /*  Function to add each row */
  56. int Addrow(int array1[10][10], int k, int c)
  57. {
  58.     int rsum = 0, i;
  59.     for (i = 0; i < c; i++)
  60.     {
  61.         rsum = rsum + array1[k][i];
  62.     }
  63.     return(rsum);
  64. }
  65. /*  Function to add each column */
  66. int Addcol(int array1[10][10], int k, int r)
  67. {
  68.     int csum = 0, j;
  69.     for (j = 0; j < r; j++)
  70.     {
  71.         csum = csum + array1[j][k];
  72.     }
  73.     return(csum);
  74. }
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
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 Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Matrix, go to C Programming Examples on Matrix. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.