156. Design a function to find largest and second largest element of a given array.
#include<stdio.h>
int
a[10]={2,2,5,8,15,20,45,23,1,3};
int
l,sl,ele;
void
l_sl(int *,int ,int *,int *);
void
main()
{
ele=sizeof(a)/sizeof(a[0]);
l_sl(a,ele,&l,&sl);
printf("%d %d\n",l,sl);
}
void
l_sl(int *p, int ele,int *q,int *r)
{
int l=0,sl=0,i;
if(p[0]==p[1])
{
p[1]=p[1]^p[2];
p[1]=p[1]^p[2];
p[1]=p[1]^p[2];
}
if(p[0]>p[1])
{
l=p[0];
sl=p[1];
}
if(p[1]>p[0])
{
l=p[1];
sl=p[0];
}
for(i=2;i<ele;i++)
{
if(p[i]>l)
{
sl=l;
l=p[i];
}
else if(p[i]>sl && p[i]!= l)
{
sl=p[i];
}
}
*q=l;
*r=sl;
}
Comments
Post a Comment