Return maximum value function

Hi guys so i'm creating this function to return the maximum value of an array, I have looked over everything but it still doesn't return the max value.

int max(int array[], int n){
int max = array[0];
for(int i=0; i<n; i++)
{
if (array[i] > max){
max = array[i];
}
return max;
}
}
You have a function and a var with the name max, which could be a problem.
Also you need to put your return statement outside the loop.

Easiest solution:
1
2
3
4
5
#include <algorithm>
int maxValue(int array[], int n)
{
  return *std::max_element(array, array + n);  
} 

Thank you, that worked perfectly.
Topic archived. No new replies allowed.