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


Please Help Members By Posting Answers For Below Questions

difference between Low, Middle, High Level languages in c ?

1634


Simplify the program segment if X = B then C ← true else C ← false

2587


How can I change the size of the dynamically allocated array?

632


what is a NULL Pointer? Whether it is same as an uninitialized pointer?

758


If the size of int data type is two bytes, what is the range of signed int data type?

594






find the output? void r(int a[],int c, int n) { if(c>n) { a[c]=a[c]+c; r(a,++c,n); r(a,++c,n); } } int main() { int i,a[5]={0}; r(a,0,5); for(i=0;i<5;i++) printf("\n %d",a[i]); getch(); }

1859


Can we declare function inside main?

570


What does %p mean c?

632


How can you restore a redirected standard stream?

610


What is the difference between strcpy() and memcpy() function in c programming?

626


Why c is a procedural language?

584


exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above

665


general for is %wd,f-d; in this system "w" means a) 'w' represent total width of digits b) 'w' represent width which includes the digits before,after decimal place and the decimal point c) 'w' represent width which includes the digits before only d) 'w' represent width after decimal place only

588


How can you convert integers to binary or hexadecimal?

616


Why c language?

648