Home »
C++ Programs
C++ program to find the second smallest number in the array using class
Submitted by Shubh Pachori, on August 25, 2022
Problem statement
Given an array, we have to find the second smallest number in the array using the class and object approach.
Example:
Input:
array[0]:1
array[1]:2
array[2]:44
array[3]:3
array[4]:5
Output:
Second Smallest Number is 2
C++ code to find the second smallest number in the array using the class and object approach
#include <iostream>
using namespace std;
// create a class
class Array {
// private data member
private:
int array[5];
// public functions
public:
// getArray() function to get
// the value of the array
void getArray() {
for (int index = 0; index <= 4; index++) {
cout << "array[" << index << "]:";
cin >> array[index];
}
}
// secondSmallest() function to
// find out the second
// smallest number in the array
int secondSmallest() {
// initialising int type variables
// to perform operations
int index_1, index_2, temp, second;
// for loop to read the whole array
for (index_1 = 0; index_1 <= 4; index_1++) {
// for loop to compare numbers of array
for (index_2 = 0; index_2 < 4 - index_1; index_2++) {
// if condition to check if the next term is smaller than
// this then swapping takes place
if (array[index_2] > array[index_2 + 1]) {
// swapping numbers if numbers
// are not in the order
temp = array[index_2];
array[index_2] = array[index_2 + 1];
array[index_2 + 1] = temp;
}
}
}
// considering array[0] is the
// second smallest number
second = array[0];
// for loop for traversing of array
for (int index = 0; index <= 4; index++) {
// if value at second is greater than array[index]
if (second < array[index]) {
// then value at array[index] is copied to second
second = array[index];
// break the statement after this operation
break;
}
}
// returning second smallest number
return second;
}
};
int main() {
// create an object
Array A;
// function is called by the object
// to store the array
A.getArray();
// int type variable to store
// the second smallest number
int second;
// secondSmallest() function is called
// by the object to find out
// the second smallest number in the array
second = A.secondSmallest();
cout << "Second Smallest Number in array is " << second;
return 0;
}
Output
array[0]:33
array[1]:333
array[2]:11
array[3]:1111
array[4]:44
Second Smallest Number is 33
Explanation
In the above code, we have created a class Array, one int type array data member array[5] to store the values, and public member functions putArray() and secondSmallest() to store the given values in an array and to find the second smallest number.
In the main() function, we are creating an object A of class Array, reading integer values by the user of the array using the putArray() function, and finally calling the secondSmallest() member function to find out the second smallest number in the given integer number in the array. The secondSmallest () function contains the logic to find out the second smallest number in the given numbers and printing the result.
C++ Class and Object Programs (Set 2) »