A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

Row sum & Column sum of Matrix: C program

In this C program we are calculating the sum of each row and sum of each column of the matrix.
Example:
matrix =
1 2 3
4 5 6
7 8 9

Sum of 1st, 2nd and 3rd row elements are 6, 15 and 24 respectively.
Sum of 1st, 2nd and 3rd column elements are 12, 15 and 18 respectively.

Description:
i & j – Loop control variables.
m – Number of Rows of matrix.
n – Number of Columns of matrix.
mat – Input matrix.
sumr – Sum of each row.
sumc – Sum of each column.

Algorithm to find the Row sum and Column sum of matrix

STEP 1: START
STEP 2: INITIALIZE the values of Row sum and Column sum to ZERO
              sumr = 0, sumc = 0
STEP 3: READ number of rows and columns
              READ m, n 
 STEP 4: READ the matrix
              LOOP from i = 0 to m
              LOOP from j = 0 to n
              READ mat(i)(j)
STEP 5: CALCULATE the sum of each row and print the sum
              LOOP from i = 0 to m
              LOOP from j = 0 to n
              ADD the row elements and print
              sumr = sumr + mat(i)(j) 
              PRINT sumr
              SET sumr = 0
STEP 6: CALCULATE the sum of each column and print the sum
              LOOP from j = 0 to n
              LOOP from i = 0 to m
              ADD the column elements and print
              sumc = sumc + mat(i)(j)
              PRINT sumc
              SET sumc = 0
STEP 7: END

C Program to find the Row sum and Column sum of matrix

#include<stdio.h>
#include<conio.h>
void main ()
{
 
    int mat[10][10];
    int i, j, m, n, sumr = 0, sumc = 0;
    printf("Enter the order of the matrix: ");
    scanf("%d %d", &m, &n);
    printf("Enter the elements of the matrix:\n");
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++) 
           scanf("%d", &mat[i][j]);
    for (i = 0; i < m; i++) 
    {
        for (j = 0; j < n; j++) 
            sumr = sumr + mat[i][j] ;
        printf("Sum of the %d row is = %d\n", i, sumr);
        sumr = 0;
    }
    for (j = 0; j < n; ++j) 
    {
    for (i = 0; i < m; ++i)
    sumc = sumc + mat[i][j];
    printf("Sum of the %d column is = %d\n", j, sumc);
    sumc = 0;
    }
 }

Explanation:
In the above program, we are using the 2 Dimensional arrays to store the matrices. Using nested for loop we are taking the input of matrices.
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
sumr = sumr + mat[i][j] ;
printf(“Sum of the %d row is = %d\n”, i, sumr);
sumr = 0;
}
Using for loop we are calculating the row sum of the matrix. After calculating the sum of each row we must set the value of row sum to zero that is, sumr = 0.
Similarly we can find out the column sum also with the same steps.

Output:

Enter the order of the matrix: 2 3
Enter the elements of the matrix:
2 4 2
9 5 7
Sum of the 0 row is = 8
Sum of the 1 row is = 21
Sum of the 0 column is = 11
Sum of the 1 column is = 9
Sum of the 2 column is = 9

QUIZ:

1] Matrices are stored using.
  1. 1 – D Array.
  2. 2 – D Array.
  3. Any array.
  4. Multi dimensional Arrays.