# C Program to Find Largest and Smallest in 2d Array with Their Position

Program description:- Write a C program to find the largest and smallest in a 2d array of numbers with their position or location. Take an array of numbers as input, find the largest and smallest element among them, and display the result.

To write this program, first, we need to take the largest and smallest variable, which will be used to compare with all array elements. We can initialize them with 0, but it will be valid only if the array contains only positive numbers.

``````// wrong
largest = 0;
smallest = 0;``````

If we are initializing smallest variable with 0 and array contains only positive numbers then 0 always will be the smallest among them, and we won’t find the correct smallest element. Similarly if largest is initialized with 0 and array contains only -ve numbers then we will never find the correct largest numbers.

``````// assume first element is
// largest and smallest
largest = arr;
smallest = arr;``````

Due to above problem, better to assign first element of array to the smallest and largest variable then compare it with remaining elements of the array. Now, let us develop the program.

## Largest Smallest in 2d Array using C

``````#include<stdio.h>
int main()
{
int m, n, largest, smallest;
int largrowloc, largcolumnloc, smallrowloc, smallcolumnloc;

// take number of rows and columns
printf("Enter number of row and column: ");
scanf("%d %d",&m,&n);

// declare array by given number of rows and columns
int arr[m][n], i, j;

// take array elements as input
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter arr[%d][%d]: ",i,j);
scanf("%d", &arr[i][j]);
}
printf("\n");
}

// display array (optional)
printf("Entered 2D Array:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}

// assume first element is
// largest and smallest
largest=arr;
smallest=arr;

// compare with all elements
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(largest<arr[i][j])
{
largest=arr[i][j];
largrowloc=i;  //row location of largest element
largcolumnloc=j;   //column location of largest element
}

if(smallest>arr[i][j])
{
smallest=arr[i][j];
smallrowloc=i;  //row location of smallest element
smallcolumnloc=j;  //column location of smallest element
}
}
}

// display results
printf("\n"); // new line
printf("Largest element in array is %d in location arr[%d][%d]\n",
largest, largrowloc, largcolumnloc);
printf("Smallest element in array is %d in location arr[%d][%d]\n",
smallest, smallrowloc, smallcolumnloc);

return 0;
}``````

Output:-

Enter number of row and column: 3,3
Enter arr: 4
Enter arr: 5
Enter arr: 6

Enter arr: 9
Enter arr: 8
Enter arr: 7

Enter arr: 2
Enter arr: 3
Enter arr: 5

Entered 2D Array:
4 5 6
9 8 7
2 3 5

Largest element in array is 9 in location arr
Smallest element in array is 2 in location arr

In this program, after taking the array elements as input, we are displaying the array. It is optional part, if you don’t want to display then remove that part of the code.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Examples on multidimensional Array in C