Issue as newbie

Hi, Im newbie with Arduino so im trying to get used with functions, variables, and so on, I did a little sketch in order to learn to create arrays and trying to fill those variables with random numbers and finally see it in serial port, but I dont know why works until some specific number, 872 in my case, with 873 doesnt work at all. I would appreciate your inputs, and an explanation about this wierd behavior.

Thanks in advance

//WORKS FINE
int randNumber[872];

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  for (int x=0; x<872; x=x+1){
    randNumber[x] = random(872);
    Serial.println(randNumber[x]);
    delay (100);
  }
}

//DOESNT WORKS
int randNumber[873];

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  for (int x=0; x<873; x=x+1){
    randNumber[x] = random(873);
    Serial.println(randNumber[x]);
    delay (100);
  }
}

Which Arduino?
If it is a Nano, UNO etc. they have 2kB of sram. An array of 872 integers requires 1744 bytes. You have to leave room for the stack and local variables. Presumably 873 integers is pushing it just over the edge.

Pete

I agree with the answer given by el_supremo. I just wanted to add this...

  1. I notice that in your code you use the number 872 (or 873) three times. This is not considered good programming practice. It would be helpful learn to use #define or const int so that you only need to change the number in a single place.

  2. x=x+1 operates perfectly, but you may want to get into the habit of using x++ . I know it doesn't seem very useful here, but it will become more important when your variable names get longer.

To add to what vaj4088 said, you have two ways of using "symbolic" constants in a program:

#define MAGIC 872
const int MAGICNUM = 872;

int randNumber[MAGIC];

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  for (int x = 0; x < MAGICNUM; x=x+1){
    randNumber[x] = random(872);
    Serial.println(randNumber[x]);
    delay (100);
  }
}

The advantage is that if you change the constant at the top of the program, it automatically gets changed everywhere in the program. Note that the #define and const approaches have the same effect, but they are a little different. The #define is "typeless". That is, it can be used in an expression with any other type of data (e.g., char, byte, float, etc.) whereas the const is tied to the int data type. Because const does allow for type checking, which is a good thing, most programmers lean towards the const approach.

You will also see this construct:

  for (int x = 0; x < sizeof(randomNumber) / sizeof(randomNumber[0]); x=x+1){

This idiom means that the second expression in the for loop doesn't need to be changed even if you change the size of the array. The sizeof() operator returns the number of bytes allocated to whatever object appears within the parentheses. Since the randomNumber array has 1744 bytes allocated to it (872 * 2) and one element (randomNumber[0]) occupies 2 bytes, then 1744 / 2 = 872. If you change the number of elements in the array, I think you'll be able to see that the idiom correctly determines the correct ending point in the for loop.

thanks for your inputs...