C program to find Second Largest and Second Smallest element of an unsorted Array

#include<stdio.h>
#define MAX 50
int main()
{
      int n, i, max, min;
      int arr[MAX], max1, min1;

     printf("Enter the number of elements");

     scanf("%d",&n);

     printf("Enter the elements of array");

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

     max=min=arr[0];


     for(i=1; i<n; i++)

       {
           if(arr[i]>max)
                max=arr[i];

           if(arr[i]<min)

                min=arr[i];
        }

       max1=min1=arr[0];


       for(i=1; i<n; i++)

       {
            if(arr[i]<max && arr[i]>max1)
                max1=arr[i];

           if(arr[i]>min && arr[i]<min1)

                min1=arr[i];
        } 

      printf("Second smallest and largest element is %d %d",min1,max1);


return 0;


}






Post a Comment

0 Comments