C++ Program to Find the Second Largest Number in an Array

0 0
Read Time:31 Second

C++ Program to Find the Second Largest Number in an Array

#include <iostream>
using namespace std;
 
int secondLargest(int arr[], int len) {
 
    //Initialize
    int max        = INT_MIN;
    int second_max = INT_MIN;
 
     for(int i = 0; i < len; i++) {
 
         if(arr[i] > max) {
              second_max = max;
              max = arr[i]; 
          }
 
          if(max > arr[i] && arr[i] > second_max) {
               second_max=arr[i];
           }
      }
 
      return second_max;
 }
 
int main(void) {
 
     int arr[] = {70, 4, 8, 10, 14, 9, 7, 6, 5, 3, 2};
     int len   = 11;
 
      cout<<"Second highest element is "<<secondLargest(arr,len))<<endl;
      return 0;
};
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment