Hi, is there any way to look for a value within an array?
(In pseudocode)
If array[0] = 10,
Look for first instance of the number 21 within the array and print the number associated with the placement in the array.
Hi, is there any way to look for a value within an array?
(In pseudocode)
If array[0] = 10,
Look for first instance of the number 21 within the array and print the number associated with the placement in the array.
One way is to use a for loop to iterate through the array comparing each element with the number that you want to find. When the number is found, the for loop index will equal the array index of the searched for number.
int array[] = {1, 2, 3, 4, 5, 6, 7, 21, 8, 9, 10};
int searchNumber = 21;
int numberIndex = 0;
void setup()
{
Serial.begin(9600);
for (int n = 0; n < sizeof(array) / sizeof(array[0]); n++)
{
if (array[n] == searchNumber)
{
numberIndex = n;
Serial.print("number index = ");
Serial.println(numberIndex);
break;
}
}
}
void loop()
{
}