Iterate Through a 2D Array using For Loop in C
In C, a 2D array is a collection of elements arranged in rows and columns. To iterate through a 2D array using a for loop, we use nested loops: the outer loop iterates through rows, while the inner loop iterates through columns. This allows us to access each element systematically and perform operations like printing, modifying, or computing values.
Examples of Iterating Through a 2D Array
1. Printing Elements of a 2D Array
In this example, we will declare a 2D array and use a nested for loop to traverse and print each element row by row.
main.c
#include <stdio.h>
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Loop through rows
for (int i = 0; i < 2; i++) {
// Loop through columns
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Explanation:
- We declare a 2D array
matrixwith 2 rows and 3 columns, initialized with values. - The outer
forloop (variablei) iterates over the rows. - The inner
forloop (variablej) iterates over the columns. printf()prints each element, and a newline is printed after each row.
Output:
1 2 3
4 5 6
2. Computing the Sum of a 2D Array
In this example, we will iterate through a 2D array and compute the sum of all its elements.
main.c
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
// Loop through the 2D array to compute sum
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
printf("Sum of all elements: %d\n", sum);
return 0;
}
Explanation:
- We declare a
3x32D arraymatrixwith initialized values. - A variable
sumis initialized to0to store the total. - The outer loop iterates through the rows, and the inner loop iterates through the columns.
- Each element is added to
suminside the inner loop. - Finally,
printf()displays the total sum.
Output:
Sum of all elements: 45
3. Finding the Maximum Element in a 2D Array
In this example, we will iterate through a 2D array and find the largest element.
main.c
#include <stdio.h>
int main() {
int matrix[2][4] = {{10, 25, 15, 30}, {45, 5, 35, 20}};
int max = matrix[0][0]; // Initialize with the first element
// Loop through the 2D array to find the maximum element
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
}
printf("Maximum element: %d\n", max);
return 0;
}
Explanation:
- We declare a
2x42D arraymatrixwith values. - A variable
maxis initialized with the first element. - The outer loop iterates through the rows, and the inner loop iterates through the columns.
- If the current element is greater than
max, we updatemax. - After the loop completes,
printf()displays the maximum element.
Output:
Maximum element: 45
Conclusion
In this tutorial, we explored how to iterate through a 2D array using nested for loops in C. We covered:
- Printing elements row by row.
- Computing the sum of all elements.
- Finding the maximum element in a 2D array.
Using nested for loops, we can efficiently process 2D arrays for various operations in C programming.
