Check Array Values

I got an array for calibration which contains a specific value if not calibrated.

I need to check a certain part of the Array if it contains this value somewhere.

whats the easiest way?

Do I need to check each single value by a for loop or is there a simpler way?
How to leave the loop once the value is encountered?

You need to go through the array indeed, if data in the array is not ordered then start from index 0 and plan to go all the way to last index in the array and if you find your value use the break statement to exit the for loop.

Thanks I assumed ther must be an existing function for thin in C

I missed this bit initially and posted the same, so removed my reply. :slight_smile: :-

use the break statement to exit the for loop.

Gorkde:
Thanks I assumed ther must be an existing function for thin in C

If you put the value that indicates calibration has not occurred in a particular place in the array you wouldn't need to search through looking for it. Perhaps that could be the first, or even the last, value in the array. Is that possible?

coincidentally, I was asking this same problem the other day...

so anyway after some google search, I found this snippet of code

uint8_t arraySearch(uint8_t a[], uint8_t array_length, uint8_t seek_value)
{
  //const uint8_t array_length = sizeof (a) / sizeof ( uint8_t );
  for ( uint8_t i = 0; i < array_length; i++)
  {
    if ( a[i] == seek_value ) return i;
  }
  return -1;

}

so basically to use, You need to tell it 3 parameter, One is the array you are using... The size of the array, and lastly, the value that you are seeking...

in my case i am more interested in the location of the value i serch.. but for you maybe you are waiting for the -1 value that the fucnction return. Basically if return value is not -1, it mean your system have not been calibrated.

uint8_t arraySearch(uint8_t a[], uint8_t array_length, uint8_t seek_value) {
  //const uint8_t array_length = sizeof (a) / sizeof ( uint8_t );
  for ( uint8_t i = 0; i < array_length; i++) {if ( a[i] == seek_value ) return i;}
  return -1;
}

that function will actually return as soon as it finds the element and give you the index.

There is a challenge with it though. it is supposed to return an unsigned int on 8 bits (uint8_t). so not only you are limited to arrays with 256 values but -1 is not really actually something that function should return.

@J-M-L:

sorry for the error in the code, I actually edited the original code to work with my own application...

anyway it should be something like this

int8_t arraySearch(int8_t a[], int8_t array_length, int8_t seek_value)
{
  for ( int8_t i = 0; i < array_length; i++) 
  {
     if ( a[i] == seek_value ) return i;
  }
  return -1;
}

the int8_t can be change to any number of size such as int16_t, int32_t.