c++

How to Find the Second Smallest Number in an Integer Array in C++ Full Project For Beginners


 

Welcome folks today in this blog post we will be inserting a new element in an array in c++ using vector class All the full source code of the application is shown below.

 

 

 

 

 

 

In order to get started you need to make a app.cpp file and copy paste the following code

 

 

app.cpp

 

 

 

/* 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;
}

 

Similar Posts:

    None Found

Leave a Reply

Your email address will not be published. Required fields are marked *