// Using single loop for finding the second largest number in the array
# include<stdio.h>
# include<stdlib.h>
# define MAX 5
int main()
{
int nLoop;
int nMaximumFirst,nMaximumSecond;
int szArray[MAX];
// to get input
/* Please ignore this loop by direct initalization*/
for(nLoop=0;nLoop<MAX;nLoop++)
scanf("%d",&szArray[nLoop]);
nMaximumFirst = szArray[0];
nMaximumSecond = szArray[0];
for(nLoop=0;nLoop<MAX;nLoop++)
{
if(nMaximumFirst < szArray[nLoop])
{
nMaximumSecond = nMaximumFirst;
nMaximumFirst = szArray[nLoop];
}else if(nMaximumSecond < szArray[nLoop])
{
nMaximumSecond = szArray[nLoop];
}
else
{
// Do nothing
}
}
printf("Maximum first =%d,\nMaximum second = %d\n",nMaximumFirst,nMaximumSecond);
return 0;
}
OUTPUT:
4
5
3
2
1
Maximum first =5,
Maximum second = 4
Press any key to continue . . .
No comments:
Post a Comment