Find the Largest Element in a 2D Array in C

To find the largest element in a 2D array in C, we traverse all rows and columns using nested loops while keeping track of the maximum value encountered. This ensures that we examine every element and identify the highest value.


Examples to Find the Largest Element in a 2D Array

1. Finding the Largest Element Using Nested Loops

In this example, we use nested loops to iterate over each element of the 2D array and update a variable to store the largest value encountered.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int arr[3][3] = {
        {3, 8, 12},
        {5, 15, 7},
        {9, 6, 20}
    };

    int max = arr[0][0]; // Initialize with the first element

    // Traverse the 2D array
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (arr[i][j] > max) {
                max = arr[i][j]; // Update max if a larger element is found
            }
        }
    }

    printf("The largest element in the 2D array is: %d\n", max);

    return 0;
}

Explanation:

  1. We declare a 3×3 2D array arr[][] and initialize it with values.
  2. We define an integer variable max and initialize it with the first element of the array.
  3. We use nested for loops to iterate over all rows and columns.
  4. Inside the inner loop, we check if the current element is greater than max.
  5. If a larger value is found, we update max with the new largest value.
  6. Finally, we print the largest element using printf().

Output:

The largest element in the 2D array is: 20

2. Finding the Largest Element Using a Function

In this example, we define a function findLargest() to find the maximum element in a 2D array and return it.

main.c

</>
Copy
#include <stdio.h>

#define ROWS 3
#define COLS 3

int findLargest(int arr[ROWS][COLS]) {
    int max = arr[0][0];

    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (arr[i][j] > max) {
                max = arr[i][j];
            }
        }
    }

    return max;
}

int main() {
    int arr[ROWS][COLS] = {
        {4, 18, 9},
        {2, 11, 21},
        {10, 5, 16}
    };

    int max = findLargest(arr);
    printf("The largest element in the 2D array is: %d\n", max);

    return 0;
}

Explanation:

  1. We define constants ROWS and COLS for the array dimensions.
  2. We create a function findLargest() that takes a 2D array as input.
  3. The function initializes max with the first element and uses nested loops to find the largest value.
  4. The largest value is returned to the main() function.
  5. In main(), we declare and initialize a 3×3 array.
  6. We call findLargest() and store the result in max.
  7. We print the largest element using printf().

Output:

The largest element in the 2D array is: 21

Conclusion

In this tutorial, we explored different ways to find the largest element in a 2D array in C:

  1. Using Nested Loops: Directly iterates over each element to find the maximum value.
  2. Using a Function: Encapsulates the logic in a reusable function for better modularity.