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

</>
Copy
#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:

  1. We declare a 2D array matrix with 2 rows and 3 columns, initialized with values.
  2. The outer for loop (variable i) iterates over the rows.
  3. The inner for loop (variable j) iterates over the columns.
  4. 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

</>
Copy
#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:

  1. We declare a 3x3 2D array matrix with initialized values.
  2. A variable sum is initialized to 0 to store the total.
  3. The outer loop iterates through the rows, and the inner loop iterates through the columns.
  4. Each element is added to sum inside the inner loop.
  5. 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

</>
Copy
#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:

  1. We declare a 2x4 2D array matrix with values.
  2. A variable max is initialized with the first element.
  3. The outer loop iterates through the rows, and the inner loop iterates through the columns.
  4. If the current element is greater than max, we update max.
  5. 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:

  1. Printing elements row by row.
  2. Computing the sum of all elements.
  3. Finding the maximum element in a 2D array.

Using nested for loops, we can efficiently process 2D arrays for various operations in C programming.