Try using std::max_element
from <algorithm>
:
#include <algorithm>
int i[] = { 1, 84, 11, 31 };
// for C++11 and later:
int max = *std::max_element(std::begin(i), std::end(i));
// for C++03 or earlier:
int max2 = *std::max_element(i, i + (sizeof(i) / sizeof(*i)));
or if your array is static, you can just use an C++11 initializer list:
auto i = { 1, 84, 11, 31 };
int max = std::max(i);
int max2 = std::max({ 1, 84, 11, 31 });
PS: A live example using these methods can be found here.
1
solved Finding largest number in array [closed]