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

How can I remove the trailing spaces from a string?

697


which of the following statement is wrong a) mes=123.56; b) con='T'*'A'; c) this='T'*20; d) 3+a=b;

2697


plz let me know how to become a telecom protocol tester. thank you.

1843


Explain what is a program flowchart and explain how does it help in writing a program?

753


What is a example of a variable?

648






What is struct node in c?

700


What is the process to create increment and decrement stamen in c?

670


Explain what is operator promotion?

720


Want to know how to write a C program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

1617


What is the default value of local and global variables in c?

651


how to write optimum code to divide a 50 digit number with a 25 digit number??

2848


What are the 5 elements of structure?

688


simple program of graphics and their output display

1575


Where static variables are stored in memory in c?

610


How can I get the current date or time of day in a c program?

742