Find smallest and largest element in an array-C program

by

Posted on 23 February 2017

Arrays are easy just add square brackets and you get an array. At Least easier than array using linked lists anyways..

Recommended Read : Understanding the concepts of linked list the Easy Way

In this tutorial we will learn how to find out the largest and smallest element of an array by making a C program so,

Lets C the code:

#include<stdio.h>
int main()
{
    int array[100], n, smallest=0, largest=0, i;
    printf("How many numbers:\n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
smallest=array[0];
largest=array[0];    
    while(n--)
    {
        if(smallest > array[n])
            smallest = array[n];
        if(largest < array[n])
            largest = array[n];
    
    }
    
    printf("Smallest Number in the array is %d", smallest);
    printf("\nLargest Number in the array is %d", largest);
return 0;
}

Output:

Largest element in an array using C

Explanation:

Variables:
‘ array[100]’ an array of size 100 is declared, so maximum number of numbers that the array can hold is 100.
‘ n ’ stores how many numbers user wants  to insert.
‘ smallest ’ is used to hold the smallest number found in the array.
‘ largest ’ is used to hold the largest number found in the array.
‘ i ’ is a counter variable used to insert the values in the array starting from index 0 in the array.

for(i=0; i<n; i++)
{
 scanf("%d",&array[i]);
}

The above loop is used to store the numbers entered by the user into the array

while(n--)
{
 if(smallest > array[n])
     smallest = array[n];
 if(largest < array[n])
    largest = array[n];
    
}

‘smallest’ and ‘largest’ variables are given the value of first element of the array because if ‘smallest’ is initialised to 0,if all the numbers inserted in the array are greater than 1, then smallest will always be equal to 0.

The above code finds the largest and the smallest element in the array by comparing each element in the array with other.


If an element is the array[] is smaller than ‘smallest’, we assign the value of array element to ‘smallest’, similarly for ‘largest’ if an element in the array[] is larger than ‘largest’ then we store the value of array[] to ‘largest’

printf("Smallest Number in the array is %d", smallest);
printf("\nLargest Number in the array is %d", largest);

The above code prints the largest and the smallest value in the array.

The code above first stores the elements in the array and then finds the largest and smallest element in the array, which requires two loops, taking more time.

Lets C another code:

#include<stdio.h>
int main()
{
    int array[100], n, smallest=0, largest=0, i;
    printf("How many numbers:\n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    if(i==0)
    {
    smallest = array[0];
    largest = array[0];
    }
        if(smallest>array[i])
            smallest = array[i];
        if(largest < array[i])
            largest = array[i];
    }
    
    printf("Smallest Number in the array is %d", smallest);
    printf("\nLargest Number in the array is %d", largest);
return 0;
}

Output:

Largest and Smallest element in an array

Almost everything remains same, except for a small details. The logic for checking is put inside the for loop itself, so as soon as input is taken from the user, it is checked if it is the smallest/largest number inserted.

for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
    if(i==0)
    {
    smallest = array[0];
    largest = array[0];
    }
if(smallest>array[i])
smallest = array[i];
if(largest < array[i])
    largest = array[i];
}//end of for loop

The statement if(i==0) initialises the smallest and largest variable to first element inserted by the user, to cross check it with the remaining elements inserted. Next two if statements are same as before, only with their index changed from array[n] to array[i].

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts

Search
Adverstisement
Adverstisement