array function printin 23 + 1001+ 9+ 1234+ 987 = 3254

i am trying to print the 5values and also the sum the only what i see is 3254 0 0 0 0 my queastion is why does it print like that and not 23 + 1001+ 9+ 1234+ 987 = 3254 in my serial monitor

void setup() {
  int my_array[5]= {23 + 1001+ 9+ 1234+ 987};     
  int i;               
  
  Serial.begin(9600);
 int value =0;
  for (i = 0; i < 5; i++) {
    value += my_array[i];
    Serial.println(my_array[i]);
  }
}

void loop() {
}

You have a five element array with one element (the first) initialised to the sum of five constants

  int my_array[5]= {23 + 1001+ 9+ 1234+ 987};

Your array has 5 elements numbered 0 to 4 but you are only putting a value in element 0

Array values should be separated by commas

i understand i have 5 integers in one array
so i would be like int my_array[1]= {23 + 1001+ 9+ 1234+ 987}; 1 aray whit the calculation
Serial.println(my_array[1]);

int my_array[1]= {23 + 1001+ 9+ 1234+ 987};
...
Serial.println(my_array[0]);

i changed the program a bit now i got 5 times printed 3254 but not the 23 + 1001+ 9+ 1234+ 987 = 3254
do i need to make a seperate print in function

i changed the program a bit

Are you going to show it to us ?

int my_array[1]= {23 + 1001+ 9+ 1234+ 987};
...
Serial.print (F("23 + 1001+ 9+ 1234+ 987 ="));
Serial.println(my_array[0]);

(Uncompiled.Untested. Probably needs more # preprocessor)

opelvectra:
i understand i have 5 integers in one array
so i would be like int my_array[1]= {23 + 1001+ 9+ 1234+ 987}; 1 aray whit the calculation
Serial.println(my_array[1]);

True, you have an array of 5 integers but you are only giving an explicit value to my_array[0]

ill post tommorow my code atm i a reading Arrays in Arduino Sketches

You’re making an ATM?

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