C++ How To Find Min and Max in an Array
Find Min and Max in an Array
Use the min_element() and max_element() functions from the <algorithm> library:
Example
int numbers[] = {45, 12, 85, 32, 89, 39};
int size = sizeof(numbers) / sizeof(numbers[0]);
int smallest = *min_element(numbers, numbers + size);
int largest = *max_element(numbers, numbers + size);
cout << "Min: " << smallest << "\n";
cout << "Max: " << largest;
Explanation:min_element() and max_element() return a pointer to the element, not the value itself.
The * in front gets the value that the pointer points to.