A matrix is a 2D array of numbers arranged in rows and columns. Finding the sum of its elements — row by row, column by column, or all at once — is one of the most common matrix operations and a good exercise in nested loops and function design. This post shows two approaches: a direct loop for the total, then a cleaner version using separate functions for row sums, column sums, and the grand total.
Method 1 — Total Sum with Nested Loops
The simplest case: iterate every element once and accumulate a running total.
#include <stdio.h>
#define MAX 10
int main(void)
{
int a[MAX][MAX];
int rows, cols, i, j, total = 0;
printf("Enter rows and columns (max %d): ", MAX);
scanf("%d %d", &rows, &cols);
printf("Enter %d elements:\n", rows * cols);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
total += a[i][j];
printf("Sum of all elements: %d\n", total);
return 0;
}
Method 2 — Row Sums, Column Sums, and Total Using Functions
Splitting the logic into functions keeps main() readable and makes each operation independently testable. This is the structure asked for in most lab assignments.
#include <stdio.h>
#define MAX 10
int row_sum(int a[][MAX], int row, int cols);
int col_sum(int a[][MAX], int col, int rows);
int main(void)
{
int a[MAX][MAX];
int rows, cols, i, j, total = 0;
printf("Enter rows and columns (max %d): ", MAX);
scanf("%d %d", &rows, &cols);
printf("Enter %d elements:\n", rows * cols);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
scanf("%d", &a[i][j]);
/* Print the matrix */
printf("\nMatrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%4d", a[i][j]);
printf("\n");
}
/* Row sums */
printf("\nRow sums:\n");
for (i = 0; i < rows; i++)
printf(" Row %d: %d\n", i + 1, row_sum(a, i, cols));
/* Column sums */
printf("\nColumn sums:\n");
for (j = 0; j < cols; j++)
printf(" Col %d: %d\n", j + 1, col_sum(a, j, rows));
/* Grand total */
for (i = 0; i < rows; i++)
total += row_sum(a, i, cols);
printf("\nTotal sum of all elements: %d\n", total);
return 0;
}
int row_sum(int a[][MAX], int row, int cols)
{
int j, sum = 0;
for (j = 0; j < cols; j++)
sum += a[row][j];
return sum;
}
int col_sum(int a[][MAX], int col, int rows)
{
int i, sum = 0;
for (i = 0; i < rows; i++)
sum += a[i][col];
return sum;
}
How to Compile and Run
gcc -ansi -Wall -Wextra -o matrix_sum matrix_sum.c
./matrix_sum
Sample Input and Output
Input — a 3×3 matrix:
Enter rows and columns (max 10): 3 3
Enter 9 elements:
1 2 3
4 5 6
7 8 9
Matrix:
1 2 3
4 5 6
7 8 9
Row sums:
Row 1: 6
Row 2: 15
Row 3: 24
Column sums:
Col 1: 12
Col 2: 15
Col 3: 18
Total sum of all elements: 45
Code Explanation
The #define MAX 10 constant sets the fixed array dimension. Because C requires 2D array parameters to have a known second dimension at compile time, the functions are declared as int a[][MAX]. The actual number of rows and columns used is passed separately as rows and cols.
row_sum(a, i, cols) receives the row index i and scans across all cols columns in that row. col_sum(a, j, rows) receives the column index j and scans down all rows rows in that column. Both return an int sum.
The grand total is computed by calling row_sum once per row and accumulating — this reuses the function rather than adding a third loop, which keeps the code DRY.
Why not void main()? Many older textbooks use void main(), but the C standard requires int main(void). Using the correct signature avoids undefined behaviour and compiler warnings.
What This Program Teaches
- How to declare and read a 2D array in C
- Why the second dimension must be fixed in a function parameter (
a[][MAX]) - How to use nested loops to traverse rows and columns independently
- How to pass a 2D array to a function and use a return value
Related C Programs
📖 Learning C from a book? The C Programming Language by K&R (Amazon.in) · Amazon.com
\
\Preparing\ for\ a\ C\ interview\?\ Practice\ with\ the\ \C\ Programming\ Quiz\\ —\ 150+\ questions,\ free\ on\ Android.\\
1 comment on “Sum of Elements in a Matrix in C – Row, Column, and Total”
please can any one help me??
to write a program to add the elements of each column of a matrix and store it in an array and print it ??