Find the Sum of All Elements in a 2D Array in C
To find the sum of all elements in a 2D array in C, we need to iterate through each row and column, adding up all the values. This is commonly done using nested loops, where the outer loop traverses the rows and the inner loop traverses the columns, accumulating the sum in a variable.
Examples to Find Sum of Elements in a 2D Array
1. Finding Sum of All Elements Using Nested Loops
In this example, we will define a 2D array and use nested loops to traverse each element, summing them up into a variable.
main.c
#include <stdio.h>
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
// Iterating through each element using nested loops
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += arr[i][j]; // Adding each element to sum
}
}
printf("Sum of all elements: %d\n", sum);
return 0;
}
Explanation:
- We declare a
3x3
integer arrayarr
and initialize it with values. - We initialize an integer variable
sum
to store the total sum, starting at0
. - We use a nested loop where the outer loop (
i
) iterates over the rows and the inner loop (j
) iterates over the columns. - Inside the inner loop, we add each element
arr[i][j]
to thesum
variable. - After both loops finish, we print the final sum.
Output:
Sum of all elements: 45
2. Finding Sum Using a Function
In this example, we will define a function to calculate the sum of a 2D array and call it from the main
function.
main.c
#include <stdio.h>
#define ROWS 2
#define COLS 3
// Function to calculate sum of a 2D array
int sumOfArray(int arr[ROWS][COLS]) {
int sum = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
sum += arr[i][j];
}
}
return sum;
}
int main() {
int numbers[ROWS][COLS] = {
{1, 2, 3},
{4, 5, 6}
};
int total = sumOfArray(numbers);
printf("Sum of all elements: %d\n", total);
return 0;
}
Explanation:
- We define constants
ROWS
andCOLS
to specify the array dimensions. - We declare a function
sumOfArray()
that takes a 2D array as input. - Inside the function, we use nested loops to iterate through the array and sum the elements.
- In
main()
, we declare a2x3
arraynumbers
and initialize it with values. - We call
sumOfArray()
and store the returned sum intotal
. - We print the total sum.
Output:
Sum of all elements: 21
3. Finding Sum of Elements in a Dynamically Sized 2D Array
In this example, we will take user input for the number of rows and columns, dynamically allocate memory for the 2D array, and compute the sum.
main.c
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, cols;
// Taking user input for dimensions
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
// Dynamically allocating memory for 2D array
int **arr = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int));
}
// Taking user input for array elements
printf("Enter elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
int sum = 0;
// Calculating sum
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}
printf("Sum of all elements: %d\n", sum);
// Free allocated memory
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
Explanation:
- We take user input for rows and columns.
- We dynamically allocate memory using
malloc()
. - We take user input for array elements.
- We use nested loops to calculate the sum.
- Finally, we free the allocated memory.
Output:
Enter number of rows: 2
Enter number of columns: 3
Enter elements of the array:
1 2 3 4 5 6
Sum of all elements: 21
Conclusion
In this tutorial, we covered different methods to find the sum of all elements in a 2D array, including nested loops, functions, and dynamic memory allocation.