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
difference between Low, Middle, High Level languages in c ?
Simplify the program segment if X = B then C ← true else C ← false
How can I change the size of the dynamically allocated array?
what is a NULL Pointer? Whether it is same as an uninitialized pointer?
If the size of int data type is two bytes, what is the range of signed int data type?
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(); }
Can we declare function inside main?
What does %p mean c?
How can you restore a redirected standard stream?
What is the difference between strcpy() and memcpy() function in c programming?
Why c is a procedural language?
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
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
How can you convert integers to binary or hexadecimal?
Why c language?