how to find the smallest and 2nd smallest number in an array

#include <iostream>
using namespace std;

int min(int arr[], int size,  int &secound_min)
{
int min = arr[0];
int sec_min = arr[1];

for (auto i = 0; i < size; i++)
{
if (arr[i] < min)
{
sec_min = min;
min = arr[i];

}

}

secound_min = sec_min;
return min;
}

int main()
{
int arr[10] = {5,9,2,1,10,0,-1,-2,0,3};
int secound_min;
int min_vaue = min(arr,10, secound_min);

cout << min_vaue << endl << secound_min << endl;
}

Comments

Popular posts from this blog

Dynamic Memory allocation in C++ for 1d, 2d and 3d array

Write Your Own String Class:

Printing the odd and even no using Pthread