Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

. Write a c program to find out second smallest element of an unsorted array.?

 #include<stdio.h>  
 #include<conio.h>  
 void main()  
 {  
  int a[50],size,i,j=0,sm, sdsmall;  
  printf("Enter the size of the array: ");  
  scanf("%d",&size);  
  printf("Enter %d elements in to the array: ", size);  
  for(i=0;i<size;i++)  
      scanf("%d",&a[i]);  
  small=a[0];  
  for(i=1;i<size;i++)  
 {  
      if(sm>a[i])  
 {  
         sm=a[i];  
         j = i;  
    }  
  }  
  sdsmall=a[size-j-1];  
  for(i=1;i<size;i++)  
 {  
      if(sdsmall > a[i] && j != i)  
        sdsmall =a[i];  
  }  
  printf("Second smallest: %d", sdsmall);  
  getch();  
 }  
 Sample Output:  
 Enter the size of the array: 5  
 Enter 5 elements into the array: 5 7 3 2 6  
 Second smallest: 3 

 

Post a Comment

0 Comments