C++ Program to Find Second Smallest and Second Largest Element from Array
Write C++ Program to Find Second Smallest and Second Largest Element from Array
// CPP Program to Find Second Smallest and Second Largest Element from Array
#include <iostream>
using namespace std;
int main()
{
int A[] = {89, 5, 65, 32, 108},i;
int n = sizeof(A)/sizeof(A[0]), j, temp;
cout << "Array Elements :--> ";
for(i=0; i<n; i++)
cout << A[i] << " ";
// SORT ARRAY
for(i=0; i< n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
if(A[j] < A[i])
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
cout << "\n\nSmallest Element :--> " << A[0];
cout << "\nLargest Element :--> " << A[n - 1];
cout << "\n\nSecond Smallest Element :--> " << A[1];
cout << "\nSecond Largest Element :--> " << A[n - 2];
return 0;
}
Output:
Array Elements :--> 89 5 65 32 108
Smallest Element :--> 5
Largest Element :--> 108
Second Smallest Element :--> 32
Second Largest Element :--> 89