# Find the second smallest number in an integer array in C++

This C++ program is able to find the second smallest number in an integer array in C++ by using some suitable conditions. This problem is based on the concept of multi dimension array and also utilizes conditional statements. Following is a complete solution for the problem along with the source code.

## How to find the second smallest number in an integer array in C++

Problem statement

There is an array of size n with any value of elements in it. Find the second highest element in the array.

• Element values are entered by the user

Problem approach

• Declare an array of the required length.
• Store the array elements values.
• Find the required value using correct logic.
• Print the result on the screen.

#### Source code

Following program find the second smallest number in a given integer array.

```/* C++ program to find second number number in array**
***give different sizes for array & the their values to get different result***
** enter array values in the limit -2,147,483,648 to 2,147,483,647 only **
*/

#include <iostream>
using namespace std;
int main()
{
int size;
cout<<"Enter the size of array: ";
cin>>size;
int array[size];                            // array declaration

for(int i=0;i<size;i++)
cin>>array[i];                          // input array values

int smallest=2147483647;                    // maximum integer values
int secondsmall=2147483647;

for(int i=0;i<size;i++){                    // logic for smallest and second smallest value
if(smallest>array[i]){
secondsmall=smallest;
smallest=array[i];
}
if(secondsmall>array[i]&& array[i]>smallest)
secondsmall=array[i];
}

cout<<"The second smallest number present in given array is "<<secondsmall;
return 0;
}```

Example outputs

```Enter the size of array: 5
Enter array elements: 65
313
131
89
32
The second smallest number present in given array is 65
Process returned 0 (0x0) execution time : 21.583 s
Press any key to continue.```

In this, an array of size 5 is declared and perform the operation and print the output successfully.

```Enter the size of array: 8
Enter array elements: 53
23
436
467
253
15
95
68
The second smallest number present in given array is 23
Process returned 0 (0x0) execution time : 39.148 s
Press any key to continue.```

Here an array of size 8 is declared, accept values and do the requires operation.

Program explanation

1. Accept size of array from user.
2. Initialize an array using this size.
3. Accept values for the same.
4. Initialize two variables for storing the smallest and second smallest number.
5. Use for loop to traverse through the array, then find smallest and second smallest values using conditional statements with suitable conditions.
6. Print the required value on the screen.