Before going to the program first let us understand what is Second Smallest Element in an Array?

Second Largest Element in Array:

Element which is having the second smallest value in a given array is called as the Second Smallest Element in an Array.

Example: Let us consider an array of values as given below:

a[]={6,3,8,5,1,9,4};

then,

The Second Smallest Element in an Array is “3”.

Now let us see the program code for Second Smallest Element in an Array and understand the code using the Explanation given below.

## Program code to find the Second Smallest Element in an Array:

```#include<stdio.h>
#include<conio.h> void main()
{ int a[50]; int n,i,small,s_small; clrscr(); printf("n Enter number of elements: "); scanf("%d",&n); printf("n Enter %d elements: ",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } small=s_small=a[0]; for(i=1;i<n;i++) { if(small>a[i]) { s_small=small; small=a[i]; } else if(s_small>a[i] && a[i]!=small) { s_small=a[i]; } } printf("n The Second Smallest Element in the given Array: %d", s_small); getch();
}
```

## Explanation:

• First the computer reads the number of elements in the array from the user and stores it in the “n” variable using the following lines:
```printf("n Enter number of elements: ");
scanf("%d",&n);
```

Note: %d is used to read the Integer value.

• Then it reads the elements one by one using the for loop and stored in the “a” array variable respectively using the following lines:
```printf("n Enter %d elements: ",n);
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
```
• Then it assigns the value of a[0] to “small” and “s_small” variables using the following line:
```small=s_small=a[0];
```
• Then using for loop and if-else-if conditions the second smallest element is found and stored in “s_small” variable using the following lines:
```for(i=1;i<n;i++)
{ if(small>a[i]) { s_small=small; small=a[i]; } else if(s_small>a[i] && a[i]!=small) { s_small=a[i]; }
}
```
• Finally The Second Smallest Element in the given Array is printed on the screen using the following line:
`printf("n The Second Smallest Element in the given Array: %d", s_small); `

## Step by Step working of the above Program Code:

1. Let us assume that a user enters the Number of elements as 5.
2. And the user enters the Elements as {6 3 7 9 5}.
3. So it stores the elements in the array a[]={6,3,7,9,5}.
4. Now it assigns the value of a[0] to small and s_small(i.e. small=s_small=6).
5. Then it assigns the value of i=1 and the loop continues till the condition of the for loop is true.

5.1.   i<n    (1<5)    for loop condition is true

small>a[i]   (6>3)    if condition is true

s_small=small    So, s_small=6

small=a[i]    So,  small=3

i++    (i=1+1)     So  i=2

5.2.  i<n    (2<5)    for loop condition is true

small>a[i]   (3>7)   if condition is false

s_small>a[i] && a[i]!=small   (6>7 && 7!=3)    else-if condition is false

i++    (i=2+1)     So  i=3

5.3.  i<n    (3<5)    for loop condition is true

small>a[i]   (3>9)   if condition is false

s_small>a[i] && a[i]!=small   (6>9 && 9!=3)    else-if condition is false

i++    (i=3+1)     So  i=4

5.4.  i<n    (4<5)    for loop condition is true

small>a[i]   (3>5)    if condition is false

s_small>a[i] && a[i]!=small   (6>5 && 5!=3)    else-if condition is true

s_small=a[i]    So,  s_small=5

i++    (i=4+1)     So  i=5

5.5.  i<n    (5<5)    for loop condition is false

It comes out of the for loop.

1. Finally it prints The Second Smallest Element in the given Array: 5
2. Thus program execution is completed.