write a program to fined second smallest and largest element
in a given series of elements (without sorting)

Answers were Sorted based on User's Feedback



write a program to fined second smallest and largest element in a given series of elements (without..

Answer / 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

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / srijit

#include<stdio.h>
#include<conio.h>


int main()
{
int size,a[100],i,j=0,k=0,min1,min2;
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];
for(i=0;i<size;i++)
{
if(a[i]<min1)
{
min1=a[i];
j=i;
}

}
for(i=0;i<size;i++)
{
if(i!=j)
{
min2=a[i];
break;
}
}

for(i=0;i<size;i++)
{
if((i!=j)&&a[i]<min2)
{
min2=a[i];
}

}
printf("Second minimam element of the array is %d\n",min2);


getch();


}

Is This Answer Correct ?    5 Yes 1 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / sachin tyagi

void main()
{
int a[12],max,min1.min2,max2,i,temp;
printf("enter valu");
for(i=0;i<11;i++)
scanf(%d,&a[i]);
max=a[0];
min1=a[0];
for(i=1;i<11;i++)
{
if(max<a[i])
max=a[i];
if(min1>a[i];
min1=a[i]
}
for(i=1;i<11;i++0
{
if((max2>a[i])&&(max2!=max){
max2=a[i];
}
if((min2<a[i])&&(min2!=min1))
{
min2=a[i];}

print("small is %d and large is %d", min2,max2);
}

Is This Answer Correct ?    15 Yes 13 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / raj

#include <stdio.h>


void main()
{
int i, ar[] ={15,22,122,15,6,12};
int a,b= 0,c,ta ,tb, tc;
a =ar[0];
b = ar[1];

for (i = 0; i < 6 ;i++)
{
if(a > ar[i])
{
b = a;
a = ar[i];
}
else if(b > ar[i])
{
b = ar[i];
}
}

printf("\n %d ",b);
}

Is This Answer Correct ?    3 Yes 3 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / siddharth chauhan

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("Enter Array Length.");
int ArrayLength =
Convert.ToInt32(Console.ReadLine());
int[] arr = new int[ArrayLength];

Console.WriteLine("\nEnter Array Elements");
for (int i = 0; i < arr.Length; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}

Console.WriteLine("\nElements are :- ");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}

int Largest = arr[0], SecondLargest = arr[0],
Smallest = arr[0], SecondSmallest = arr[0];
for (int j = 1; j < arr.Length; j++)
{
if (Smallest > arr[j])
{
Smallest = arr[j];
}

if (Largest < arr[j])
{
Largest = arr[j];
}
}

for (int j = 1; j < arr.Length; j++)
{
int value = arr[j];

if (arr[j] < SecondSmallest && arr[j] !=
Smallest)
{
SecondSmallest = arr[j];
}

if (SecondLargest < arr[j] && arr[j] < Largest)
{
SecondLargest = arr[j];
}
}

Console.WriteLine("Largest : " + Largest);
Console.WriteLine("Second Largest : " +
SecondLargest);
Console.WriteLine("Smallest : " + Smallest);
Console.WriteLine("Second Smallest : " +
SecondSmallest);
Console.ReadLine();

}

Is This Answer Correct ?    3 Yes 4 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / nikhil kumar saraf

void main()
{
int a[12],max,min,min2,i;
printf("enter values");
for(i=0;i<10;i++)
scanf(%d,&a[i]);
max=a[0];
min=a[0];
for(i=0;i<10;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
min2=a[0];
for(i=0;i<10;i++)
{
if(min2>a[i] && a[i]!=min)
{
min2=a[i];
}
printf("The second smallest element is:-%d",min2);
printf("The largest element is:-%d",max);
getch();
}

Is This Answer Correct ?    2 Yes 4 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / vadivel t

#include<stdio.h>

void main()
{
int a[10] = {5,4,1,7,3,0,8,23,12,24};
int Lcount = 0, Gcount = 0, i, j;

for(i = 0; i<10; i++)
{
Gcount = 0;
Lcount = 0;
for(j = 0; j<10; j++)
{
if(a[i] > a[j])
{
Gcount++;
Lcount++;
}
}
if(Gcount == 8)
{
printf("The second largest no is: %
d \n", a[i]);
}
if(Lcount == 1)
{
printf("The second smallest no is: %
d \n", a[i]);
}
}
_getch();
}

Is This Answer Correct ?    2 Yes 4 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / anand

include<stdio.h>
#include<conio.h>
void main()
{
int a[10],max,min,i,temp;
printf("\n enter array element");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
//second smallest number
min=a[0];
for(i=0;i<10;i++)
{
if(min>a[i])
{
min=a[i];
}

Is This Answer Correct ?    4 Yes 12 No

write a program to fined second smallest and largest element in a given series of elements (without..

Answer / mohit kumar

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],max,min,i,temp;
printf("\n enter array element");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
//second smallest number
min=a[0];
for(i=0;i<10;i++)
{
if(min>a[i])
{
min=a[i];
}

Is This Answer Correct ?    20 Yes 39 No

Post New Answer

More C Interview Questions

The variables are int sum=10,SuM=20; these are same or different?

3 Answers  


what is difference between getchar,putchar functions and printf and scanf function? does putchar show output only when input given to it

5 Answers   DIT,


Why is event driven programming or procedural programming, better within specific scenario?

0 Answers   Wipro,


Why isn't any of this standardized in c? Any real program has to do some of these things.

0 Answers  


How do you define a function?

0 Answers  






#include<stdio.h> int main() { int a[3][3][2]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}; printf("%d\n",*(*(*a+1)); return 0; } What will be the output of the above question? And how?

1 Answers   Groupon,


Write a Program to print this triangle: * ** * **** * ****** * ******** * ********** use two nested loops.

12 Answers   MIT, TCS,


Explain 'bit masking'?

0 Answers   EXL,


What are header files and explain what are its uses in c programming?

0 Answers  


What is advantage of pointer in c?

0 Answers  


How would you obtain the current time and difference between two times?

0 Answers   TISL,


What is local and global variable in c?

0 Answers  


Categories