Create a function to find the max value in array - C++ Data Type

C++ examples for Data Type:Array

Description

Create a function to find the max value in array

Demo Code

#include <iostream>
using namespace std;
int findMax(int [], int);  // function prototype
int main()/*ww  w  . java2s .  com*/
{
   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;
}

Result


Related Tutorials