130. Wap to find 2nd largest element from given integer array .
#include<stdio.h>
int a[10]={20,20,20,20,3,15,15,5,21,19},ele,i,l=0,sl=0;
void main()
{
ele=sizeof(a)/sizeof(a[0]);
printf("\n");
for(i=0;i<ele;i++)
printf("%d ",*(a+i));
if(a[0]==a[1])
{
a[1]=a[1]^a[2];
a[2]=a[1]^a[2];
a[1]=a[1]^a[2];
}
if(a[0]>a[1])
{
l=a[0];
sl=a[1];
}
else if(a[1]>a[0])
{
l=a[1];
sl=a[0];
}
for(i=2;i<ele;i++)
{
if(a[i]>l)
{
sl=l;
l=a[i];
}
else if(a[i]>sl && a[i]!=l)
{
sl=a[i];
}
}
printf("\n");
printf("Largest Number : %d\n",l);
printf("Second Largest : %d\n",sl);
}
Comments
Post a Comment