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
Write a program to swap two numbers without using the third variable?
Suppose we have a table name EMP as below. We want to perform a operation in which, I want to change name ‘SMITH’ from as ‘SMITH JAIN’. Also I want to change the name of the column from ENAME to E_NAME. EMPNO ENAME JOB MGR HIREDATE SAL 7369 SMITH Coder 7902 17-DEC-80 800 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 7521 WARD SALESMAN 7698 22-FEB-81 1250
What is sizeof int?
A program is required to print your biographic information including: Names, gender, student Number, Cell Number, line of study and your residential address.
Write a C program to count the number of email on text
any "C" function by default returns an a) int value b) float value c) char value d) a & b
What is file in c preprocessor?
Draw a diagram showing how the operating system relates to users, application programs, and the computer hardware ?
Why is it usually a bad idea to use gets()? Suggest a workaround.
Explain argument and its types.
What is the difference between array_name and &array_name?
write a program to print the consecutive repeated character from the given string... input string is : hhhhjkutskkkkkggggj output should be like this: hhhhkkkkkgggg anyone help me...
How can you invoke another program from within a C program?
Explain what are multibyte characters?
Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.