First Maximum & Minimum, Second Maximum & Minimum in Array

Previously we already wrote a program to find the first maximum and the first minimum of n numbers. Now this time we will write a C program to find the first maximum and minimum Second maximum and minimum in the array

Program description:- Write a program to find the first maximum, first minimum, second maximum, and second minimum in the given list of N numbers. Prerequisites:- Array in C, Smallest and the largest number in an array

First & Second Maximum and Minimum Element in Array of Numbers

include<stdio.h>
int main()
{
   int a[100], n, i, fmax, smax, fmin, smin;

   printf("Enter array size [1-100]: ");
   scanf("%d",&n);

   printf("Enter %d elements: ",n);
   scanf("%d",&a[0]);
   fmax=fmin=a[0];

   // finding first minimum and maximum
   for(i=1; i<n; i++)
   {
     scanf("%d",&a[i]);
     if(fmax<a[i]) fmax=a[i];
     if(fmin>a[i]) fmin=a[i];
   }

   // assigning second minimum and maximum
   smax=fmin;
   smin=fmax;

   // finding second minimum and maximum
   for(i=0; i<n; i++)
   {
     if(a[i]<fmax && a[i]>smax) smax=a[i];
     if(a[i]>fmin && a[i]<smin) smin=a[i];
   }

   printf("First maximum: %d",fmax);
   printf("\nSecond maximum: %d",smax);
   printf("\nFirst minimum = %d",fmin);
   printf("\nSecond minimum = %d",smin);

   return 0;
}

Output for the different test-cases:-

Enter array size [1-100]: 5
Enter 5 elements: 1 2 3 4 5
First maximum: 5
Second maximum: 4
First minimum = 1
Second minimum = 2

Enter array size [1-100]: 5
Enter 5 elements: -9 -8 -7 -6 -5
First maximum: -5
Second maximum: -6
First minimum = -9
Second minimum = -8

Enter array size [1-100]: 5
Enter 5 elements: 0 1 2 0 1
First maximum: 2
Second maximum: 1
First minimum = 0
Second minimum = 1

Explanation of this program

To find the first maximum and minimum we declared two variables fmax and fmin, and initialize both of them with the first element of an array. Now, using for loop we find the first maximum and first minimum element. If an array element is greater than fmax than assign it to fmax, similarly if an array element is lesser than fmin than assign it to fmin. When for loop execution completed we got first maximum and first minimum element of the array.

Now, we use two variables smax and smin for second maximum and minimum. In C language, every variable should be assigned any value before using that variable. If we not assigned any value to variable then it holds garbage value and at the comparison time that garbage value may be greater or lesser than an element with which we are comparing. By this way, we will get an unexpected result.

In this program, we initialize smax=fmin and smin=fmax. The logic is that the second maximum element will be never lesser than fmin in the array and second minimum element will be never greater than fmax in the array. If the array element is greater than smax and lesser than fmax then update smax with this element of the array. Similarly, if the array element is greater than fmin and lesser than smin then update smin with this array element. Finally, the results displayed on the screen.

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

Leave a Comment

Your email address will not be published. Required fields are marked *