function proplem

hi i need help with this exercise max of 5 numbers using function




That should be simple I think:

1
2
3
4
5
6
7
8
9
int max(int a, int b, int c, int d, int e)
{
       int max = a;
       max = b > a ? b : a;
       max = c > max ? c : max;
       max = d > max ? d : max;
       max = e > max ? e : max;
       return max;
}


consider using vector btw :)
C++11 actually already have such a function.
std::max({2, 8, 9, 4, 6}); // returns 9
Failing that, I'd prefer a method that takes an array and doesn't contain ternary operations.

Each to their own, though. :-)
@iHutch105, in that case you can use std::max_element.
1
2
int arr[5] = {2, 8, 9, 4, 6};
std::max_element(arr, arr + 5); // returns 9 
does std::max accept containers as well?

It looks like std::max_element works with iterators so yes isn'it?
@Peter87, I like it.
Topic archived. No new replies allowed.