I'm doing a problem to print the array again in reverse order, but it keeps showing that "egArray was not declared in this scope". Can anyone help me?
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int egArray[10]={8,7,6,3,2,1,5,4,9,10};
int num;
}
void loop() {
// put your main code here, to run repeatedly:
for ( int index=egArray.length - 1; index > =0 ; index--) {
Serial.println(egArray[index]);
}
}
your array only exists inside the setup() function by its a local variable.
To use it in other functions it needs to be declared as global.
Study about variables.
Here is also giving compilation error: index=egArray.length - 1
.lengh is used to "Strings" .
"length() - Arduino Reference"
I modified it to look like this: sizeof(egArray) -1
As the array is of "int", and int in arduino occupies 2, the size of your array will be 20
try this code:
// int egArray[10] = {8,7,6,3,2,1,5,4.9,10}; // <<<<<--------- . between 4 and 9 my fault
int egArray[10] = {8,7,6,3,2,1,5,4,9,10};
void setup() {
Serial.begin(115200);
delay(100);
}
void loop() {
//for(int index = sizeof(egArray)/2; index >= 0; index--)
for(int index = (sizeof egArray / sizeof (int)) -1; index >= 0; index--)
{Serial.println(egArray[index]);
}
}
int egArray[10]={8,7,6,3,2,1,5,4,9,10};
int num;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
for ( int index=egArray.length - 1; index > =0 ; index--) {
Serial.println(egArray[index]);
}
}
WARNING: 'sizeof' returns the size IN BYTES. Your 9-int array has a size of 18 (or 36 on a 32-bit processor). You want: for(int index = (sizeof egArray / sizeof (int)) -1; index >= 0; index--)
WARNING: Your 'int' array contains a 4.9 which will be truncated to 4.