write a program to fined second smallest and largest element
in a given series of elements (without sorting)
Answer Posted / pradeep
/*No doubt this works fine, execute and see*/
#include<stdio.h>
#include<conio.h>
void main()
{ int size,a[100],i,j=0,k=0,min1,min2,max1,max2;
printf("Input size of an array\n");
scanf("%d",&size);
printf("Input the %d elements of the array\n",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
min1=a[0];
max1=a[0];
for(i=0;i<size;i++)
{
if(a[i]<min1)
{
min1=a[i];
j=i;
}
if(a[i]>max1)
{
max1=a[i];
k=i;
}
}
for(i=0;i<size;i++)
{
if(i!=j)
{
min2=a[i];
break;
}
}
for(i=0;i<size;i++)
{
if(i!=k)
{
max2=a[i];
break;
}
}
for(i=0;i<size;i++)
{
if((i!=j)&&a[i]<min2)
{
min2=a[i];
}
if((i!=k)&&a[i]>max2)
{
max2=a[i];
}
}
printf("Second minimal element of the array is %d\n",min2);
printf("Second maximal element of the array is %d\n",max2);
getch();
}
| Is This Answer Correct ? | 13 Yes | 4 No |
Post New Answer View All Answers
What are global variables and explain how do you declare them?
Why functions are used in c?
What is hashing in c language?
Do pointers need to be initialized?
The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?
What is wild pointer in c?
Tell me the use of bit field in c language?
What is actual argument?
What is the function of multilevel pointer in c?
What is a lookup table in c?
What is a null pointer in c?
What is a node in c?
How to set file pointer to beginning c?
Can the sizeof operator be used to tell the size of an array passed to a function?
What is build process in c?