Find out second largest element of an unsorted array?
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[100],i,num,n,mx;
printf("enter number of elements to be entered");
scanf("%d",&n);
printf("enter numbers");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mx=a[0];
num=a[0];
for(i=0;i<n;i++)
{
if(a[i]>num)
{
if(a[i]>mx)
{num=mx;
max=a[i];}
if(a[i]<mx)
num=a[i];
}
}
printf("second largest no. is %d",num);
system("pause");
}
No comments:
Post a Comment