Q:

C Program to find the second smallest element in an array

0

C Program to find the second smallest element in an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>

void main()
{
  int arr1[50],n,i,j=0,sml,sml2nd;
  
   
       printf("Input the size of array : ");
       scanf("%d", &n);
    /* Stored values into the array*/
       printf("Input %d elements in the array (value must be <9999) :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }
		
   sml=arr1[0];
  for(i=0;i<n;i++)
  {
      if(sml>arr1[i])
	  {
           sml=arr1[i];
           j = i;
      }
  }

		
   sml2nd=99999;
  for(i=0;i<n;i++)
  {
     if(i==j)
        {
          i++;  /* ignoring the smallest element */
		  i--;
        }
      else
        {
          if(sml2nd>arr1[i])
	     {
               sml2nd=arr1[i];
             }
        }
  }

  printf("The Second smallest element in the array is :  %d \n\n", sml2nd);
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now