function to find max value in array in cpp code example
Example 1: find min and max in array c++
#include<iostream>
using namespace std;
public void getMax_MinValue(int arr[])
{
int max, min;
max = arr[0];
min = arr[0];
for (int i = 0; i < sizeof(arr); i++)
{
if (max < arr[i])
max = arr[i];
else if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max;
cout << "Smallest element : " << min;
}
Example 2: max element in array c++ stl
*max_element (first_index, last_index);
ex:- for an array arr of size n
*max_element(arr, arr + n);