Advertisement
Shailrshah

Second Largest and Second Smallest Elements in an Array

Apr 19th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4.         int n,i,j,temp,a[100];
  5.         printf("Enter the number of elements: ");
  6.         x: scanf("%d",&n);
  7.         if(n<2)
  8.         {
  9.                 printf("No. of elements must be greater than 1: ");
  10.                 goto x;
  11.         }
  12.         printf("Enter %d elements.\n",n);
  13.         for(i=0; i<n; i++)
  14.                 scanf("%d",&a[i]);
  15.         for(i=0;i<n-1;i++)
  16.         {
  17.                 for(j=0; j<n-i-1; j++)
  18.                 {
  19.                         if(a[j]>a[j+1])
  20.                         {
  21.                                 temp = a[j];
  22.                                 a[j] = a[j+1];
  23.                                 a[j+1] = temp;
  24.                         }
  25.                 }
  26.         }
  27.         printf("Second-Smallest number is %d",a[1]);
  28.         printf("\nSecond-Largest number is %d",a[n-2]);
  29.         fflush(stdin);
  30.         getchar();
  31.         return 0;
  32. }
  33. //Output
  34. //Now enter 5 elements:-
  35. //1
  36. //2
  37. //3
  38. //4
  39. //5
  40. //Second Smallest number is 2
  41. //Second Largest is 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement