Index of Array with Max Value

Hallo everybody,

I´m a bit confused:

I exactly used this example,

http://forum.arduino.cc/index.php/topic,41999.0.html#lastPost

to geht the Index of my Array with max value and other users confirmd, that this example will do ist.
But if I use it in my code:

int ls[3]={0};

int getIndexOfMaximumValue(int* array, int size){
  int maxIndex = 0;
  int max = array[maxIndex];
  for (int i=1; i<size; i++)
   {
    if (max<array)     <---- Error in this Line  ISO C++ forbids comparison between pointer and integer
    {
      max = array;
      maxIndex = i;
    }
  }
  return maxIndex;
} 

void setup() 
{}

void loop() 
{
  
 
  ls[0] = analogRead(A0);
  ls[1] = analogRead(A1);
  ls[2] = analogRead(A2);
  ls[4] = analogRead(A3);
  
 int a=getIndexOfMaximumValue(ls,4);
    
}

It says ISO C++ forbids comparison between pointer and integer...

I still have problems using pointers.
Thanks a lot for your help!

The example you took wasn't posted with code tags and the forum software broke it. Should be:

    if (max<array[i])     <---- Error in this Line  ISO C++ forbids comparison between pointer and integer
    {
      max = array[i];
      maxIndex = i;
    }

Not sure why your index starts at 1 rather than 0 though.

I just copied that from the old forum thread.
But I Think, you ar right. Should start with 0... :cold_sweat: But that wont solve the Problem...

I don't think putting four values into a three element array is such a bright idea.

if (max<array)

If that really is your code, then the compiler is absolutely correct.
Did you forget the subscript?

that wont solve the Problem...

No, but wildbill's post will, did you read that?

Also this

  int ls[3]={0};
  ...
  ls[4] = analogRead(A3); // loading the 5th byte of a 3-byte array!!

is a problem. And other places your consider the array to be 4 bytes in size.


Rob

Of course you are right, The Array was too small. Ich changed it.

Thanks everybody for help, I havent seen the tiny [ i ] in wildbill´s Post. Now everything works fine!

I´m tyring to kalibrate some Phototransistors, to follow an light soure. First results:

There are 4 (should be 6 but only had 4) in a hexagonal box, arranged concentric and each of it is shielded to the others.
After I found the Index with the brightest Sensor (that was the reason for my question above) and a second if loop if the right or left one is brighter, I get the angle to 30°... After that I will do a calibration with Gauss squares error method. I hope to get the angle of my "lightsource" within a error of a few degrees... ( I did that with once with a Multi-hole probe for wind cancels)

Turn your face to the sun, the shadows will fall behind you.... kind of working title. :slight_smile:

Not sure why your index starts at 1 rather than 0 though.

Because the 0 case is the one you have initialised with and there is no point comparing it to itself:

  int maxIndex = 0;
  int max = array[maxIndex];
  for (int i=1; i<size; i++)