I have an array called numbers and I want to test if a certain number is included in that array.
void loop(){
if (arrayIncludeElement(numbers, mynumber)){
// Do something
} else {
// Do something else
}
}
I have written a version of the arrayIncludeElement function, but it don't seem to work.
boolean arrayIncludeElement(array, element) {
for (int i = 0; i < sizeof(array); i++) {
if (array[i] == element) {
return true;
}
}
return false;
}
ERROR MESSAGES
- sketch:5: error: 'array' was not declared in this scope
- sketch:5: error: 'element' was not declared in this scope
- sketch:5: error: initializer expression list treated as compound expression
- sketch:3: error: redefinition of 'boolean arrayIncludeElement'
- sketch:5: error: 'boolean arrayIncludeElement' previously defined here
- sketch.ino: In function 'void loop()':
- sketch:11: error: 'arrayIncludeElement' cannot be used as a function
- sketch.ino: At global scope:
- sketch:16: error: redefinition of 'boolean arrayIncludeElement'
- sketch:5: error: 'boolean arrayIncludeElement' previously defined here
- sketch:16: error: 'array' was not declared in this scope
- sketch:16: error: 'element' was not declared in this scope
EDIT I updated the function to this.
boolean arrayIncludeElement(int array[], int element);
boolean arrayIncludeElement(int array[], int element) {
for (int i = 0; i < (sizeof(array)/4); i++) {
if (array[i] == element) {
return true;
}
}
return false;
}
But now I get this error
- sketch.ino: In function 'void loop()':
- sketch:8: error: 'arrayIncludeElement' was not declared in this scope
- sketch:10: error: expected `;' before '}' token
Line 8 being this
if (arrayIncludeElement(numbers, 5){
and Line 10 being this
} else {