Write a C Program to find the Sum of each column in a Matrix. Or, write a C program to find the Sum of columns in a Multi-Dimensional Array with an example.
C Program to find sum of each column in a Matrix Example 1
This program allows the user to enter the total number of rows and columns in a Matrix. Next, we are going to calculate the sum of matrix columns using C For Loop.
/* C Program to find Sum of columns in a Matrix */ #include<stdio.h> int main() { int i, j, rows, columns, a[10][10], Sum; printf("Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); printf("Please Enter the Matrix Row and Column Elements \n"); for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { scanf("%d", &a[rows][columns]); } } for(rows = 0; rows < i; rows++) { Sum = 0; for(columns = 0; columns < j; columns++) { Sum = Sum + a[columns][rows]; } printf("The Sum of Column Elements in a Matrix = %d \n", Sum ); } return 0; }
In this Program, we declared Two-dimensional arrays of the size of 10 * 10. printf ask the user to enter the Matrix size (rows & columns. For instance 3 Rows, 3 Columns = a[3][3] ). Next, we used for loop to iterate each cell present in a[3][3] matrix. scanf inside for loop store user-entered values as array element such as a[0][0], a[0][1], …..
In this C Program to find Sum of each column in a Matrix example, User inserted values are: a[3][3] = {{10, 20, 30}, { 12, 22, 33}, {30, 40, 50}}
Row First Iteration: for(rows = 0; rows < 3; 0++) – The condition (0 < 3) is True.
Sum = 0
Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (columns < 3) is True. So, it will start executing the statements inside the loop
Sum = Sum + a[columns][rows]
Sum = Sum + a[0][0] => 0 + 10 = 10
Column Second Iteration: for(columns = 1; 1 < 3; 1++) – Condition True
Sum = Sum + a[1][0] => 10 + 12 = 22
Column Third Iteration: for(columns = 2; 2 < 3; 2++) – Condition True
Sum = Sum + a[2][0] => 22 + 30 = 52
Next, the value of the column incremented to 4. Condition (columns < 3) will fail. So it will exit from the loop. Next, we used C Programming Printf statement to print the Sum. After this, rows value will increment to 1, and the Sum will become 0.
Repeat the above steps for rows = 1 and rows = 2
C Program to find sum of each column in a Matrix Example 2
This sum of matrix columns program is the same as above, but this time we separated the code using functions.
/* C Program to find Sum of columns in a Matrix */ #include<stdio.h> void AddColumns(int arr[10][10], int i, int j) { int rows, columns, Sum = 0; for(columns = 0; columns < j; columns++) { for(rows = 0; rows < i; rows++) { Sum = Sum + arr[rows][columns]; } printf("The Sum of Column Elements in a Matrix = %d \n", Sum ); } } int main() { int i, j, rows, columns, a[10][10]; printf("Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); printf("Please Enter the Matrix Row and Column Elements \n"); for(rows = 0; rows < i; rows++) { for(columns = 0; columns < j; columns++) { scanf("%d", &a[rows][columns]); } } AddColumns(a, i, j); return 0; }
Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Row and Column Elements
10 20 30
25 35 45
54 43 56
The Sum of Column Elements in a Matrix = 89
The Sum of Column Elements in a Matrix = 187
The Sum of Column Elements in a Matrix = 318