Easiest way to find the max value stored in an array
What is the easiest way to find the maximum value stored in an array and display it on the screen...
Here's what I worked out, don't know if it works, because I don't have a compiler installed on this computer...
Code:
#include <iostream>
using namespace std;
void MaxElement (int Array [], int& Max);
const int ArraySize = 3;
int main (void)
{
int max;
int Array [3];
Array [0] = 102;
Array [1] = 230;
Array [2] = 191;
max = Array [0];
MaxElement (Array, max);
cout << "Max Value: " << max << endl;
return 0;
}
void MaxElement (int Array [], int& max)
{
for (int i = 1; i < ArraySize; i ++)
{
if (Array [i] > max)
max = Array [i];
}
}
Re: Easiest way to find the max value stored in an array
Quote:
Originally posted by criticalerror
What is the easiest way to find the maximum value stored in an array and display it on the screen...
Well, as you're clearly using C++, why not just use max_element from <algorithm>? That would appear to me to be the easiest way:
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int myArray[10] = {1,27,99,3,15,28,50,62,0,88};
cout << *max_element(myArray, myArray+10) << endl;
}
Re: Re: Easiest way to find the max value stored in an array
Quote:
Originally posted by Omnius
Well, as you're clearly using C++, why not just use max_element from <algorithm>? That would appear to me to be the easiest way:
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int myArray[10] = {1,27,99,3,15,28,50,62,0,88};
cout << *max_element(myArray, myArray+10) << endl;
}
Ah, nice, I didn't know that function already existed. That is extremely useful. Thanks.