Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

2D Array

Patterns

Excersises


C Program to Find the Maximum Element in a 2D Array


Here's a example of C program to find the maximum element in a 2D array.

Example : find_the_maximum_element_in_2darray.c
#include <stdio.h>

int main() {
  int rows, cols, max;
  
  // Input dimensions of the  2d array
  printf("Enter the number of rows: ");
  scanf("%d", &rows);
  printf("Enter the number of columns: ");
  scanf("%d", &cols);
  
  int array[rows][cols];
  
  // Input elements of the 2d array
  printf("Enter the elements of the array:\n");
  for (int i = 0; i < rows; i++) {
   for (int j = 0; j < cols; j++) {
    printf("Enter element at [%d][%d]: ", i, j);
    scanf("%d", &array[i][j]);
   }
  }
  
  // Initialize max with the first element of the array
  max = array[0][0];
  
  // Find the maximum element
  for (int i = 0; i < rows; i++) {
   for (int j = 0; j < cols; j++) {
    if (array[i][j] > max) {
         max = array[i][j];
    }
   }
  }

  // Display the maximum element
  printf("The maximum element in the 2D array is: %d\n", max);
  
  return 0;
}

Output:

Enter the number of rows: 2
Enter the number of columns: 2
Enter the elements of the array:
Enter element at [0][0]: 15
Enter element at [0][1]: 20
Enter element at [1][0]: 9
Enter element at [1][1]: 19
The maximum element in the 2D array is: 20