Find maximum & minimum Array element - C Program

Write a C program to find the second maximum and second minimum element from an array along with their positions.

Solution:

Method:
For finding the second maximum and second minimum element, we will sort the array in ascending order. After sorting the array, the second element from the beginning is the second maximum and second last element is the second minimum element.

Program

#include<conio.h>
#include<stdio.h>
#define max 100
void maxmin(int[],int);
void main()
{
    int a[max],i,n;
    printf("enter the limit of array: ");
    scanf("%d",&n);
    printf("enter the elements: ");
    for(i=0;i<n;i++)
    {
         scanf("%d",&a[i]);
    }
    maxmin(a,n);
    getch();
}
void maxmin(int a[max],int n)
{
    int i,b[max],t,j;
    for(i=0;i<n;i++)
    {
         b[i]=a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(b[j]>b[j+1])
            {
                t=b[j];
                b[j]=b[j+1];
                b[j+1]=t;
            }
        }
    }
    for(j=0;j<n;j++)
    {
         if(a[j]==b[1])
         {
              printf("\nThe second minimum element is %d and its position is:  %d ",a[j],j);
         }
         if(a[j]==b[n-2])
         {
              printf("\nThe second maximum element is %d and its position is: %d",a[j],j);
         }
     }
}


Output:

second minimum maximum