[SOLVED] Problem assigning values to array

Hi all!

I’m using an arduino mega with adruino IDE 1,8,5 for a project and I’m assigning static values to a 2d float array. While working on the code, I find the last value assigned to the array isn’t correct, it’s missing the decimal value. In order to seek help here on the forum, I cut the code down to the bare basics. And the results are even more puzzling than the missing decimal value. Here’s my code…

float wire_amps[8][2];


void setup() {

  Serial.begin(9600);
  
  Serial.println(); Serial.println("setup");  
  Serial.println();
  
  // put your setup code here, to run once:
  init_wire_amps();   // initialize wire_amps array
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("loop running\n");
  delay(1000);
  
  
}


void init_wire_amps() {
  // initalize wire_amps array


  wire_amps[0][0] = 8.4;  wire_amps[0][1] = 15;
  wire_amps[1][0] = 6.4;  wire_amps[1][1] = 10.9;
  wire_amps[2][0] = 4.8;  wire_amps[2][1] = 8.2;
  wire_amps[3][0] = 3.8;  wire_amps[3][1] = 6.3;
  wire_amps[4][0] = 2.9;  wire_amps[4][1] = 4.5;
  wire_amps[5][0] = 2.21; wire_amps[5][1] = 3.4;
  wire_amps[6][0] = 1.67; wire_amps[6][1] = 2.6;
  wire_amps[7][0] = 1,24; wire_amps[7][1] = 1.95;
  wire_amps[8][0] = 0.92; wire_amps[8][1] = 1,47;



  for (int i = 0; i <= 8; i ++){
    
    Serial.print("hi ");
    
    wire_amps[i][2] = ((wire_amps[i][0] - wire_amps[i][1])/ -400);


    delay(1000);
    
  }

  return;
  
}

Pretty straight forward, to me. Yet when the code is run on my mega, the results printed in the terminal window is not what I expect. In the terminal window, the only data printed is ‘set’ followed by some spaces and reverse question marks.

I know corrupted data printed to the terminal window can mean a mis match in baud between the arduino and the terminal window, mine is set correctly.

I also tried the above code on an arduino uno, and the results are different data printed to the terminal, but it’s still garbage data that is printed.

Anyone have an idea? or anycone offer any help figuring this out?

Thanks,
Randy

wire_amps[8][0] = 0.92; wire_amps[8][1] = 1,47;

Array indices in C run from 0 to N-1. So row 8 of your array doesn't exist. You're writing to memory that you don't own. Results will be unpredictable.

The last entry has a comma in it.

wire_amps[8][1] = 1,47;

The infamous C comma operator, bane of newbs.

In C and C++, the expression 2 + 3 has the value 5. + is an [u]operator[/u]. Likewise, , is also an operator. It takes two values, and the result is always the value of the right-hand operand. What's the use of that? Well, bot operands are always [u]evaluated[/u]. If they do something that has [u]side-effects/u, then those side-effects happen.

This is quite distinct from using the comma in an initializer, or in the argument list in a function call. In those case, the compiler interprets the comma as a delimiter. Here, however, the comma is part of an expression and will be interpreted as an operator.

The compiler is interpreting this expression like so:

[nobbc](wire_amps[8][1] = 1), (47);[/nobbc]

Both sub-expressions get evaluated. The first operand is an assignment, which gets executed. The second operand is the constant 47. The value of the whole expression is 47, but nothing gets done with that value and that's ok.

Oh BTW, @gfvalvo is also correct about writing past array bounds. The usual symptom of this is that your sketch crashes or behaves in mind-bogglingly unpredictable ways. That isn't happening here (yet), but is likely to happen if your sketch becomes more complicated with more global variables.

The infamous C comma operator, bane of newbs.

I know the comma operator is quite often abused. But, I've never seen a valid use for it. Does anyone have a realistic example where the comma operator does something useful?

DKWatson: The last entry has a comma in it.

PaulMurrayCbr: wire_amps[8][1] = 1,47;

The infamous C comma operator, bane of newbs.

This is it! That's what the problem was, a comma instead of a period. Oh, and I didn't know that a comma is an operator. While I'm a noob at C, can't say that I ever came across the use of a comma as an operator in any other language I've played with....

gfvalvo: Array indices in C run from 0 to N-1. So row 8 of your array doesn't exist. You're writing to memory that you don't own. Results will be unpredictable.

Yes, this bit caused the whole code to not work in my example code. Over-sizing the array and the code worked.

Thanks all for helping this noob out!

Randy

I think at this point you should go back to your posting, edit it and attached SOLVED to the subject. Keeps people like me from continually open it to see progress.