C++ Function Definition to find the max value in array

Description

C++ Function Definition to find the max value in array

#include <iostream>
using namespace std;
int findMax(int [], int);  // function prototype
int main()/*from w ww  . j av  a2  s .co  m*/
{
   const int MAXELS = 5;
   int nums[MAXELS] = {2, 18, 1, 27, 6};
   cout << "The maximum value is " << findMax(nums, MAXELS) << endl;
   return 0;
}
// Find the maximum value
int findMax(int vals[], int numels)
{
   int i, max = vals[0];
   for (i = 1; i < numels; i++) {
      if (max < vals[i]) {
         max = vals[i];
      }
   }
   return max;
}



PreviousNext

Related