Write a C program to find the sum of each row and column of a given square matrix (2D array). To find the sum, the inputs required are, the size of the square matrix along with the elements of the square matrix. The resultant output desired at the end is the sum of each row and column in the matrix.
Before writing the logic, we have to first gather all the necessary inputs for the given problem statement. To read these inputs at runtime, we can use scanf() function. This predefined input function reads inputs of any primitive datatype at runtime from the keyboard.
The datatype of input is determined by the format specifier mentioned and since all our inputs are of integer type, we use ‘%d’ format specifier for them all. We first read the size of the square matrix (2D array) and create the array of the same size dynamically by using calloc() function of the “stdlib.h” library.
Then, we iterate through all the rows and columns to read the elements of the array.
printf(“\nEnter the N value for square matrix :”);
scanf(“%d”,&n);
a= (int **)calloc(n,sizeof(int*));
for (i=0; i<n; i++)
a[i] =(int *)calloc(n,sizeof(int*));
printf(“\nEnter the elements in %dX%d matrix :”,n,n);
for (i = 0; i<n;i++) {
for(j=0;j<n; j++) {
scanf(“%d”,&a[i][j]);
}
}
Then, we display this input matrix on the console screen using predefined output function, printf(). This is displayed in matrix format with every row in a new line for better understanding of the user and to avoid any confusion.
We will create two variables sum1 and sum2 to store the sum of rows and columns respectively. Then, we iterate in loop and for every iteration, we traverse to the end of that respective row(a[i][j]) and column(a[j][i]) and, add the elements of the corresponding row in the sum1 variable which is initially zero for each iteration and column elements in sum2 variable which is also initially zero for each iteration.
This way, we can calculate the sum of reach row and column (eg: first row and column, second row and column,etc) at the same time. By the completion of each iteration, the sum1 contains the sum of elements of that row while sum2 contains the sum of that column.
for (i=0;i<n;i++) {
sum1=0;
sum2=0;
for(j=0;j<n;j++) {
sum1+=a[i][j];
sum2+=a[j][i];
}
printf(“sum of row %d is %d\n”,i+1,sum1);
printf(“sum of column %d is %d \n”,i+1,sum2);
}
Program – Sum of Each Row and Column of the Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <stdio.h> #include <stdlib.h> int main() { int n,i, j,**a ,sum1,sum2; printf("\nEnter the N value for square matrix :"); scanf("%d",&n); a= (int **)calloc(n,sizeof(int*)); for (i=0; i<n; i++) a[i] =(int *)calloc(n,sizeof(int*)); printf("\nEnter the elements in %dX%d matrix :",n,n); for (i = 0; i<n;i++) { for(j=0;j<n; j++) { scanf("%d",&a[i][j]); } } printf("\ngiven matrix :\n"); for (i = 0; i < n;i++) { for (j = 0; j < n; j++) { printf("%d\t", a[i][j]); } printf("\n"); } for (i=0;i<n;i++) { sum1=0; sum2=0; for(j=0;j<n;j++) { sum1+=a[i][j]; sum2+=a[j][i]; } printf("sum of row %d is %d\n",i+1,sum1); printf("sum of column %d is %d \n",i+1,sum2); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Enter the N value for square matrix :3 Enter the elements in 3X3 matrix : 1 2 3 4 5 6 7 8 9 given matrix : 1 2 3 4 5 6 7 8 9 sum of row 1 is 6 sum of column 1 is 12 sum of row 2 is 15 sum of column 2 is 15 sum of row 3 is 24 sum of column 3 is 18 |
Using Function
In the earlier method, we have seen the logic to find the sum of each row and column in the given input matrix. One thing that can be observed is that, the entire code is within main method itself. Due to this, if same logic/functionality is required elsewhere in the code later, we’ll have to rewrite it.
Also, since everything is inside main method only, it could be confusing at times to find some particular part.
So, to make the code reusable as well as to enhance the readability of the code, we can use functions.
On the basis of their functionality/logic we can split the code into parts and place them within separate function blocks.
By doing so, if same functionality is necessary elsewhere, we can just make a function call hence, making it reusable. Also, now since the code is split and placed separately the code is lot cleaner and more readable as well.
To implement this, we can first read the size of matrix and create a 2D array of the same size. Then, we can place the part to read the elements of the array in input() function and call it. The displaying of array in matrix format can be placed within another function, print() and can be called to display the input matrix.
To calculate the sum of each row and column of the given matrix, the logic can be placed with MaxColumnRow() function and can be invoked.
This way, placing each functionality in the code in separate user-defined function block makes the code reusable as each of this function can be called as many times as required and also increases the readability of the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <stdio.h> #include <stdlib.h> void MaxColumnRow(int **a,int n) { int i, j,sum1,sum2; for (i=0;i<n;i++) { sum1=0; sum2=0; for(j=0;j<n;j++) { sum1+=a[i][j]; sum2+=a[j][i]; } printf("sum of row %d is %d\n",i+1,sum1); printf("sum of column %d is %d \n",i+1,sum2); } } void print(int **arr1,int n) { int i,j; for (i = 0; i<n;i++) { for (j = 0; j<n; j++) { printf("%d\t", arr1[i][j]); } printf("\n"); } } void input(int **arr1,int n) { int i,j; for (i = 0; i<n;i++) { for(j=0;j<n; j++) { scanf("%d",&arr1[i][j]); } } } int main() { int n,i, j,**a ,max; printf("\nEnter the N value for square matrix :"); scanf("%d",&n); a= (int **)calloc(n,sizeof(int*)); for (i=0; i<n; i++) a[i] =(int *)calloc(n,sizeof(int*)); printf("\nEnter the elements in %dX%d matrix :",n,n); input(a,n); printf("\ngiven matrix :\n"); print(a,n); MaxColumnRow(a,n); return 0; } |