# biggest in an array

in a program I'm using i have a very large array around [180] is there is an easy way to find the largest member of the array , as a standard if function if bigger etc would be a huge amount of code?

No, it wouldn't be a huge amount of code.
And no, a 180 element array is not very large.

as a standard if function if bigger etc would be a huge amount of code?

Only if you don't know how to write code.

Its actually incredibly simple. You will need a loop of some kind if your not using the loop function. What you can do is, with that “loop” you can cycle through the array, while it is in the max() function. Now the max function will need to variables to compare, and the larger variable will be the output.
Example:
x= max(a, b);

What you can then do, is set the output “x” to 0 (globally at the top of your sketch, NOT within the loop itself). Then compare your array to the output. Which ever is bigger, will update “x”

for(byte idx = 0; idx < 180; idx++)
{
x = max( array[ idx ], x );
}
print the output variable “x” afterwards.

Cheers Hazards Minds,

i tried that and got it to work

more explicit initialization of the maxValue;

``````maxValue = array[0];  // initialize the maximum as the first element

for(byte idx = 1; idx < 180; ++idx)
{
if ( array[ idx ] > maxValue)
{
maxValue = array[idx];
}
}
``````

Another option would be to sort the array and take the last element.

robtillaart:
sort the array ...

This will inevitably be a more expensive solution though, in terms of coding time and execution cost. I don't think there is any easier or simpler way than iterating through the array and comparing each element with the current optimum.

very true,

but depending of OP's other goals and how the array is filled there exist other solutions.

• if the array is the result of measurements then one can test for the maximum after each reading.
• if the sketch also need the max but 1 or minimum etc sorting might help
• if the values go up and down but adjacent measurement do not differ much one can find the maximum with less tests
• don't know if more than one maximum can occur

Depending on how the code is structured, you could know the maximum value in the array as soon as you’ve filled the array.

AWOL:
Depending on how the code is structured, you could know the maximum value in the array as soon as you've filled the array.

It's basically the same algorithm(s) but evaluating at a different time though, isn't it? I mean, you could sort the array as you fill it or maintain a record of the maximum value as you append data to the array, but that doesn't change the fundamental algorithms.

It’s basically the same algorithm(s) but evaluating at a different time though, isn’t it?

Absolutely, but sometimes it’s worth just pausing and thinking about what you’re doing and how

Example: Imagine you’d been tasked with finding the maximum and minimum pixels values in a digitised monochrome image, of say, 1024 pixels by 2048 pixels as fast as possible.
Simple but almost certainly wrong on many architectures

``````for (int row = 0; row < 1024; row++) {
for (int col = 0; col < 2048; col++) {
if (image [row][col] > max)
max = image [row][col];
if (image [row][col] < min)
min = image [row][col];
}
}
``````

Much quicker to histogram the image, and find the positions of the first and last non-empty bins.

PeterH:

AWOL:
Depending on how the code is structured, you could know the maximum value in the array as soon as you've filled the array.

It's basically the same algorithm(s) but evaluating at a different time though, isn't it? I mean, you could sort the array as you fill it or maintain a record of the maximum value as you append data to the array, but that doesn't change the fundamental algorithms.

Not the fundamental algorithm, but it can affect the performance and sometimes even the footprint of the code