C++ Program to Find Second Largest and Smallest Elements of an Array

This is a C++ Program to Find the second largest/Smallest Number in an Array.

Problem Description

The program takes an array and finds the second largest/smallest number in it.

Problem Solution

1. The program takes an integer array.
2. Using nested for loops, the array is first sorted.
3. Then, the second largest and second smallest element in the sorted array is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find the second largest/Smallest Number in an Array. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int A[10], n, i, j, x;
  6.     cout << "Enter size of array : ";
  7.     cin >> n;
  8.     cout << "Enter elements of array : ";
  9.     for (i = 0; i < n; i++)
  10.         cin >> A[i];    
  11.     for (i = 0; i < n; i++)
  12.     {
  13.         for (j = i + 1; j < n; j++)
  14.         {
  15.             if (A[i] < A[j])
  16.             {
  17.                 x = A[i];
  18.                 A[i] = A[j];
  19.                 A[j] = x;
  20.             }
  21.         }
  22.     }
  23.     cout << "Second largest number : " << A[1];
  24.     cout << "\nSecond smallest number : " << A[n - 2];
  25.     return 0;
  26. }
Program Explanation

1. The user is asked to enter the array size and stored in the variable ‘n’.
2. The array elements are stored in the array ‘A’.
3. Using nested for loops, the array is sorted in ascending order so that second largest/smallest numbers can be easily retrieved.
4. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter size of array : 4
Enter elements of array : 150 69 741 0
Second largest number : 150
Second smallest number : 69
 
Case 2 :
Enter size of array : 5
Enter elements of array : 26 38 75 12 91
Second largest number : 75
Second smallest number : 26
 
Case 3 :
Enter size of array : 3
Enter elements of array : 1 2 3
Second largest number : 2
Second smallest number : 2

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.