Sunday, 31 December 2017

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;


}






No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...