C++ USES custom functions to find the second largest number in an array of integers

  • 2020-04-02 03:06:45
  • OfStack

This example shows how C++ can find the second largest number in an array of integers by using a custom function. Share with you for your reference. The specific implementation method is as follows:


const int MINNUMBER = -32767 ;
//Int 0x8000-1 of 2 bytes,
//4 bytes of Int 0x80000000- 1-2147483647
int find_sec_max( int data[] , int count)
{
  int maxnumber = data[0] ;
  int sec_max = MINNUMBER ;
  for ( int i = 1 ; i < count ; i++)
  {
    if ( data[i] > maxnumber )
    {
     sec_max = maxnumber ;
     maxnumber = data[i] ;
    }
    else
    {
     if ( data[i] > sec_max )
     sec_max = data[i] ;
    }
  }
  return sec_max ;
}

Hope that the article described in the C++ programming to help you.


Related articles: