Find the Smallest Element in a 2D Array in C

To find the smallest element in a 2D array in C, we need to traverse all elements of the array, compare them, and keep track of the smallest value found. This can be achieved using nested loops, where we iterate through each row and column to find the minimum element.


Examples to Find the Smallest Element in a 2D Array

1. Find the Smallest Element in a 2D Array Using Nested Loops

In this example, we will traverse a 2D array using nested loops and find the smallest element by comparing each element with a variable that holds the current minimum value.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int arr[3][3] = {
        {5, 12, 7},
        {3, 9, 15},
        {8, 2, 4}
    };
    
    int min = arr[0][0]; // Initialize min with the first element

    // Loop through the 2D array
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (arr[i][j] < min) {
                min = arr[i][j]; // Update min if a smaller element is found
            }
        }
    }

    printf("The smallest element in the 2D array is: %d\n", min);
    return 0;
}

Explanation:

  1. We declare a 3×3 integer array arr with predefined values.
  2. We initialize a variable min with the first element of the array arr[0][0].
  3. We use two nested for loops to iterate through each element in the 2D array.
  4. Inside the inner loop, we compare each element with min. If the current element is smaller, we update min.
  5. After the loops complete, we print the smallest element stored in min.

Output:

The smallest element in the 2D array is: 2

2. Find the Smallest Element in a User-Input 2D Array

In this example, we will allow the user to input a 2D array and then determine the smallest element dynamically.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows, cols;
    
    // Ask the user for the size of the 2D array
    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &rows, &cols);
    
    int arr[rows][cols];

    // Input elements into the array
    printf("Enter the 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 min = arr[0][0]; // Initialize min with the first element

    // Find the smallest element
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (arr[i][j] < min) {
                min = arr[i][j];
            }
        }
    }

    printf("The smallest element in the 2D array is: %d\n", min);
    return 0;
}

Explanation:

  1. We declare rows and cols to store the number of rows and columns, respectively.
  2. We use scanf() to take user input for the number of rows and columns.
  3. We declare a 2D array arr[rows][cols] to store the user-input values.
  4. We use nested loops to take input for each element of the array.
  5. We initialize min with the first element arr[0][0].
  6. We iterate over the 2D array again, updating min whenever a smaller value is found.
  7. Finally, we print the smallest element found in the 2D array.

Output Example:

Enter the number of rows and columns: 2 3
Enter the elements of the array:
4 7 1
9 2 6
The smallest element in the 2D array is: 1

Conclusion

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

  1. Using Nested Loops: We iterated through each element to find the minimum value.
  2. Using User Input: We allowed the user to enter a 2D array dynamically and found the smallest element.