Write a program for finding the 1st largest, 2nd largest and 1st smallest, 2nd smallest elements from a list of elements
#include<stdio.h>
#include<stdlib.h>
int firstLarge(int list[],int size)
{
int l1=list[0],i;
for(i=0;i<size;i++)
{
if(list[i]>l1)
l1=list[i];
}
return l1;
}
int secondLarge(int list[],int size,int l1)
{
int l2=list[0],i;
for(i=0;i<size;i++)
{
if(list[i]>l2&&list[i]<l1)
l2=list[i];
}
return l2;
}
int firstSmall(int list[],int size)
{
int s1=list[0],i;
for(i=0;i<size;i++)
{
if(list[i]<s1)
s1=list[i];
}
return s1;
}
int secondSmall(int list[],int size,int s1,int l1)
{
int s2=l1,i;
for(i=0;i<size;i++)
{
if(list[i]>s1)
{
if(list[i]<s2)
s2=list[i];
}
}
return s2;
}
int main()
{
int n,l1,l2,s1,s2,*list,i;
printf("ENTER THE LIST SIZE = ");
scanf("%d",&n);
list=(int*)malloc(n*sizeof(int));
printf("ENTER THE ELEMENT ");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
l1=firstLarge(list,n);
printf("\nFIRST LARGEST ELEMENT IS = %d",l1);
l2=secondLarge(list,n,l1);
printf("\nSECOND LARGEST ELEMENT IS = %d",l2);
s1=firstSmall(list,n);
printf("\nFIRST SMALLEST ELEMENT IS = %d",s1);
s2=secondSmall(list,n,s1,l1);
printf("\nSECOND SMALLEST ELEMENT IS = %d",s2);
}
OUTPUT :
Post a Comment
0 Comments