tech C Program Sum of Each Row and Column of A Matrix | C Programs
Latest :

C Program Sum of Each Row and Column of A Matrix | C Programs

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

Output:

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.

tech
x

Check Also

C Program To Find Maximum Between Three Numbers | C Programs

C Program to find maximum between three numbers –  In this article, we will detail ...