# How to find the index of the maximum value in an array? [closed]

Say we have an array named `myMeasurements`

``````int myMeasurements[9]={3,4,8,12,7,2,1,67,8};
``````

How can I find the index of the maximum element of this array?

For example the MATLAB code would be:

``````myMeasurements = [3,4,8,12,7,2,1,67,8];
[maxValue,Index] = max(myMeasurements);
``````

where `maxValue` is returned as 67 whereas the `Index` is returned as 8.

For the record, I am using Teensy 3.2 and I program it with the Arduino IDE using the Teensyduino add-on.

First of all, do you really need int values for the measurements or not? If not, and you have numbers between 0 and 255, switch the values to byte, your microcontroller will thank you.

Then, there is a problem with the other answers, which is... The variables cannot be left uninitialized!!! You can use this code to get the info you need:

(I also put the size of myMeasurements in a const variable.. You can thank me later for this)

``````const byte maxMeasurements = 9;
int myMeasurements[maxMeasurements ]={3,4,8,12,7,2,1,67,8};

byte maxIndex = 0;
int maxValue = myMeasurements[maxIndex];

for(byte i = 1; i < maxMeasurements; i++)
{
if(myMeasurements[i] > maxValue) {
maxValue = myMeasurements[i];
maxIndex = i;
}
}
``````
• The content of the `myMeasurements` array is coming from the ADC (`analogRead`) and hence it is 10 bits (0-1023). Unless the `map` function is used, they cannot be declared as a `byte`. I refrained from doing this because the `map` function reduces the resolution and it is likely that the values stored in the `myMeasurements` array are close to each other, i.e. the resolution plays an important role when finding the maximum element. I can define it as `unsigned int` Instead of `int`.
– csg
Jul 9, 2018 at 15:06
• As a matter of fact, I am going to use the `analogReadResolution(16)` command to increase the ADC precision to 16-bits, which is also supported by the `unsigned int` data type.
– csg
Jul 9, 2018 at 15:16
• @canberk_gurel you don't need a `map` (which is slow), you can just do something like `(uint8_t)(readAnalog(channel) >> 4)` (I don't remember the function, so forgive if `readAnalog` is not correct). In any case yes, if you need more than 8-bit measurement you need a more-than-8-bit variable, and so `uint16_t` is perfect Jul 9, 2018 at 16:04
``````int myMeasurements[9] = {3,4,8,12,7,2,1,67,8};
int maxI = 0;
for(int i = 1;i < 9;i++)
if(myMeasurements[i] > myMeasurements[maxI])
maxI = i;
``````
• Put >= for last found max Jul 9, 2018 at 9:35
• Beaten by a few seconds... Even if a little of explanation why this is better than the previous answers would not have hurt... Jul 9, 2018 at 9:38
• You don't use variable for current number, you don't use variable for max value, only for max index. You can assign 0 to maxI before for loop and so skip 1 iteration so it's faster a little bit and takes less ram Jul 10, 2018 at 10:13

Arduino forum has discussed finding the index once you know which value's index you want. Solution was arrived at by traversing the array by value you were looking for.
For your requirement, you will have to
(1) find the maxValue first and then
(2) find the position(index) of that maxValue using the code they have discussed here : https://forum.arduino.cc/index.php?topic=121116.0

• So two passes in the array to get the max index? Uhm... Jul 9, 2018 at 9:37

The code given by Jakub Krnáč gets a slight change which will give the index of the max value. Here is the changed code:

``````int myMeasurements[9]={3,4,8,12,7,2,1,67,8};
int max,current, maxI;
for(int i = 0;i < 9;i++){
current = myMeasurements[i];
if(current > max){
max = current;
maxI = i;
}
}
``````
• Oh, those wonderful uninitialized variables.... Jul 9, 2018 at 9:39