Page 2 of 2

Re: NXC array declaration

Posted: 01 Oct 2012, 20:57
by ricardocrl
The problem also exists without dimensions specified. I will then post my example. Maybe it is another problem, but also affecting the initialization.

This is the code example:

Code: Select all

float myArray[][] = {  {0.0, 1.0, 0.0}, {0.0, 0.5, 1.0} };

task main(void){
  ClearScreen();
  NumOut(0, LCD_LINE1, myArray[0][0]);
  NumOut(20, LCD_LINE1, myArray[0][1]);
  NumOut(40, LCD_LINE1, myArray[0][2]);

  NumOut(0, LCD_LINE2, myArray[1][0]);
  NumOut(20, LCD_LINE2, myArray[1][1]);
  NumOut(40, LCD_LINE2, myArray[1][2]);
  
  while(true);
}
myArray[1][2] ends up "0", instead of "1.0". In fact, from some more tests, I got:

What doesn't work(for myArray[1]):
{0.0, 0.5, 1.0}
{0.0, 0.5, 1}
{0.0, 0.5, 2.0}
{0.0, 0.5, 3}
What works - correctly initialized and displayed:
{0.0, 0.5, 1.1}
{0.0, 0.5, 1.5}
{1, 2, 3}
{0.0, 0.5, 3}

I did most of this tests using Optimization level 0, 1 and 2 and also tried with FW 1.2.9, 1.3.1 and the latest 1.3.2 (from August 2012).

I could be missing something...

Re: NXC array declaration

Posted: 02 Oct 2012, 02:22
by afanofosc
Hmm, it does look like floating point array initialization is broken if the values are a mixture of whole numbers (such as 1.0) and real numbers (such as 1.5). It looks like this has been broken for a very long time.

John Hansen

Re: NXC array declaration

Posted: 14 Oct 2012, 19:16
by drdaron
This could well be a newbie problem (I am just starting on NXC) and can't seem to get the following to put anything in the Answer array (its just showing me a load of '0' values...I've been hitting my head against this for hours, rejigging the code in various ways and always following the examples. I am using the latest firmware 1.31. Any help would be much appreciated.

Code: Select all

int Answer[5][5];

task main()
{
  for (int j=0; j<5; j++)
 {
  for (int i=0; i<5; i++)
  {
        Answer[i][j] = 1+Random(8);
        NumOut(i*8,j*8,Answer[i][j]);
  }
 }
 Wait(5000);
}

Re: NXC array declaration

Posted: 14 Oct 2012, 20:19
by HaWe
your firmware 1.31 is not the latest fw and probably not the enhanced fw ;)
try this (maybe by Google translate):
http://www.mindstormsforum.de/viewtopic.php?f=25&t=3311

the good news:
with the latest BCC beta version and efw it works fine!

Re: NXC array declaration

Posted: 14 Oct 2012, 23:21
by drdaron
Thanks for the reply - i will give it a try!

Re: NXC array declaration

Posted: 15 Oct 2012, 01:57
by afanofosc
Just to clarify, while NXC supports multi-dimensional arrays, the standard LEGO firmware does not work correctly with arrays of more than 1 dimension due to a bug in the REPLACE opcode. That bug is fixed in the enhanced NBC/NXC firmware. With the standard firmware you will not be able to use anything but 1-dimensional arrays. An array of strings is actually a 2-dimensional array since each string is an array of bytes.

If you choose to use NXC then you really should switch from the standard LEGO firmware to the enhanced NBC/NXC firmware, the latest version of which you can find here:

http://bricxcc.sourceforge.net/test_releases/

John Hansen