Find the Row-wise and Column-wise Sum in a 2D Array in C
To find the row-wise and column-wise sum in a 2D array in C, we need to traverse each row and column separately while accumulating the sum of their respective elements. We use loops to iterate through the array, computing and storing the sums for each row and column.
In this tutorial, we will explore different approaches to find the row-wise and column-wise sum with step-by-step explanations and examples.
Examples of Row-wise and Column-wise Sum
1. Find the Row-wise Sum in a 2D Array
In this example, we will compute the sum of each row in a 2D array and display the result.
main.c
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int row_sum, i, j;
// Calculate row-wise sum
for (i = 0; i < 3; i++) {
row_sum = 0;
for (j = 0; j < 3; j++) {
row_sum += arr[i][j];
}
printf("Sum of row %d = %d\n", i + 1, row_sum);
}
return 0;
}
Explanation:
- We declare a 3×3 integer array
arr[][]
with predefined values. - We initialize
row_sum
to store the sum of elements in each row. - We use a nested
for
loop: the outer loop iterates through the rows, and the inner loop iterates through the columns. - In each row iteration, we reset
row_sum
to 0 and sum the values of the row. - After summing all elements of a row, we print the sum using
printf()
.
Output:
Sum of row 1 = 6
Sum of row 2 = 15
Sum of row 3 = 24
2. Find the Column-wise Sum in a 2D Array
In this example, we will compute the sum of each column in a 2D array and display the result.
main.c
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int col_sum, i, j;
// Calculate column-wise sum
for (j = 0; j < 3; j++) {
col_sum = 0;
for (i = 0; i < 3; i++) {
col_sum += arr[i][j];
}
printf("Sum of column %d = %d\n", j + 1, col_sum);
}
return 0;
}
Explanation:
- We declare a 3×3 integer array
arr[][]
with predefined values. - We initialize
col_sum
to store the sum of elements in each column. - We use a nested
for
loop: the outer loop iterates through the columns, and the inner loop iterates through the rows. - In each column iteration, we reset
col_sum
to 0 and sum the values of the column. - After summing all elements of a column, we print the sum using
printf()
.
Output:
Sum of column 1 = 12
Sum of column 2 = 15
Sum of column 3 = 18
3. Find Both Row-wise and Column-wise Sum in a 2D Array
In this example, we will compute both row-wise and column-wise sums and display the results together.
main.c
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int i, j, row_sum, col_sum;
// Row-wise sum
printf("Row-wise sums:\n");
for (i = 0; i < 3; i++) {
row_sum = 0;
for (j = 0; j < 3; j++) {
row_sum += arr[i][j];
}
printf("Sum of row %d = %d\n", i + 1, row_sum);
}
// Column-wise sum
printf("\nColumn-wise sums:\n");
for (j = 0; j < 3; j++) {
col_sum = 0;
for (i = 0; i < 3; i++) {
col_sum += arr[i][j];
}
printf("Sum of column %d = %d\n", j + 1, col_sum);
}
return 0;
}
Explanation:
- We compute row-wise sums using nested loops.
- We print the row-wise sums.
- We compute column-wise sums using another set of nested loops.
- We print the column-wise sums.
Output:
Sum of row 1 = 6
Sum of row 2 = 15
Sum of row 3 = 24
Column-wise sums:
Sum of column 1 = 12
Sum of column 2 = 15
Sum of column 3 = 18
Conclusion
In this tutorial, we covered how to calculate row-wise and column-wise sums in a 2D array using loops.