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
How can I remove the trailing spaces from a string?
which of the following statement is wrong a) mes=123.56; b) con='T'*'A'; c) this='T'*20; d) 3+a=b;
plz let me know how to become a telecom protocol tester. thank you.
Explain what is a program flowchart and explain how does it help in writing a program?
What is a example of a variable?
What is struct node in c?
What is the process to create increment and decrement stamen in c?
Explain what is operator promotion?
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.
What is the default value of local and global variables in c?
how to write optimum code to divide a 50 digit number with a 25 digit number??
What are the 5 elements of structure?
simple program of graphics and their output display
Where static variables are stored in memory in c?
How can I get the current date or time of day in a c program?