Array outputting 4 random numbers

i have been trying to get the average value of an array for a project but when i try this:

int myarray[2];

void setup() {
myarray[0] = 1;
myarray[1] = 1;
myarray[2] = 1;

Serial.begin(115200);

for(int i = 0; i < sizeof(myarray); i++)
{
Serial.println(myarray[i]);
}

}
void loop(){

}

and my output is :
1
1
1
0
1061162752
0
1000
0
does anyone know where the 0 1061162752 0 1000 0 come from?

That array only has 2 elements, myarray[0] and myarray[1], there is no myarray[2].

int is two bytes on most arduino's, so sizeof(myarray) is 4.

use sizeof(myarray) / sizeof(myarray[0]) to get the number of array elements.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.