NXC 2D Arrays

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
logebotmaster
Posts: 6
Joined: 21 Oct 2010, 04:00

NXC 2D Arrays

Post by logebotmaster »

Hi,
Could someone please explain to me:
a) What exactly a two-dimensional array is,
b) How to modify them inside a program without errors,
and
c)What types of programs I could use them in.
I am a somewhat advanced user of NXC, but this topic is not well covered in the documentation. Thanks!
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC 2D Arrays

Post by muntoo »

It's a pointer to a pointer. ;) Actually, it is an array of an array.
For example, say you have an array of your classmate's ages:

Code: Select all

#define MYCLASS_BILL 0
#define MYCLASS_BOB 1

// int age[30];
int age[];
ArrayInit(age, 0, 30);

age[MYCLASS_BILL] = 14;
age[MYCLASS_BOB] = 15;
//Bob is one year older than Bill is. Ha, in your face, Bill!
And now you want to do all the classes in your school:

Code: Select all

#define MYCLASS 0
#define MR_KIM 1
#define MS_DODDS 2
#define DR_FALKEN 3
#define DR_YUNG 4
#define MASTER_YODA 5
#define DR_MOTTO 6
#define MR_SMILE 7
#define MR_GREGROVICH 8
#define MR_RIDER 9


// int age2[10][30];
int age2[][];
ArrayInit(age2, age, 10);
age2[MYCLASS][MYCLASS_BILL] = 14;
age2[DR_FALKEN][DAVID] = 16;


//Alternatively, you can do
int myclass[], mr_kim[], ms_dodds[], dr_falken[], dr_yung[], master_yoda[], dr_motto[], mr_smile[], mr_gregrovich[], mr_rider[];
ArrayInit(myclass, 14, 30);
myclass[MYCLASS_BOB] = 15;
ArrayInit(mr_kim, 14, 30);
ArrayInit(ms_dodds, 15, 30);
ArrayInit(dr_falken, 15, 30);
dr_falken[DAVID] = 16;
ArrayInit(dr_yung, 16, 30);
ArrayInit(master_yoda, 16, 30);
ArrayInit(dr_motto, 16, 30);
ArrayInit(mr_smile, 17, 30);
ArrayInit(mr_gregrovich, 17, 30);
ArrayInit(mr_rider, 17, 30);

ArrayBuild(age2, myclass, mr_kim, ms_dodds, dr_falken, dr_yung, master_yoda, dr_motto, mr_smile, mr_gregrovich, mr_rider); 
For multiple schools, you can use 3D arrays:

Code: Select all

int myschool[][];
myschool = age2;
int waterloo_university[][];
int jedi_school[][];


//int age3[3][10][30];
int age3[][][];
ArrayBuild(age3, myschool, waterloo_university, jedi_school);

Alternatively, you could use a 1D-Array to simulate a 2D array like this:

Code: Select all

int ArrayAccess2Di(int &arr[], unsigned int pos_e1, unsigned int pos_e2, unsigned int size_e2)
{
    return(arr[(pos_e1*size_e2)+pos_e2]);
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC 2D Arrays

Post by HaWe »

hmm - I'm not sure if that wasn't a bit too complex for an introduction to more-dim arrays....
logebotmaster
Posts: 6
Joined: 21 Oct 2010, 04:00

Re: NXC 2D Arrays

Post by logebotmaster »

:shock: Whoa! That's some confusing information (useful though -thanks!)
skelly89
Posts: 10
Joined: 13 Oct 2010, 21:41

Re: NXC 2D Arrays

Post by skelly89 »

Hello, I am having a heck of a time dealing with 2D arrays and I'm hoping somebody can illuminate my assuredly stupid blunder. I wrote a simple program that creates a 2D array, initializes all the values at 0, displays the empty array, changes some values and then displays the array again. However, when I run the program the values all stay at zero. What am I doing wrong? I have looked at the NXC documentation and a couple tutorials and as far as I can tell I'm doing it correctly.

Code: Select all

#define SIZE_X 7
#define SIZE_Y 6

int x[];
int map[SIZE_X][SIZE_Y];

void initMap()
{
     ArrayInit(x, 0, 8);
     ArrayInit(map, x, 7);
}

void displayMap()
{
     int counter = 0;
     int value;
     int i;
     
     ClearScreen();
     for(i=0; i < SIZE_X; i++)
     {
          value = map[i][5];
          NumOut(counter, LCD_LINE1, value);
          counter += 10;
          Wait(500);
     }
     counter = 0;
     for(i=0; i < SIZE_X; i++)
     {
          //value = map[i][4];
          map[i][4] = 7;
          value = map[1][4];
          NumOut(counter, LCD_LINE2, value);
          counter += 10;
          Wait(500);
     }
     counter = 0;
     for(i=0; i < SIZE_X; i++)
     {
          value = map[i][3];
          NumOut(counter, LCD_LINE3, value);
          counter += 10;
          Wait(500);
     }
     counter = 0;
     for(i=0; i < SIZE_X; i++)
     {
          value = map[i][2];
          NumOut(counter, LCD_LINE4, value);
          counter += 10;
          Wait(500);
     }
     counter = 0;
     for(i=0; i < SIZE_X; i++)
     {
          value = map[i][1];
          NumOut(counter, LCD_LINE5, value);
          counter += 10;
          Wait(500);
     }
     counter = 0;
     for(i=0; i < SIZE_X; i++)
     {
          value = map[i][0];
          NumOut(counter, LCD_LINE6, value);
          counter += 10;
          Wait(500);
     }
}

task main()
{
     initMap();
     displayMap();
     map[1][4] = 9;
     map[2][3] = 8;
     map[3][2] = 7;
     map[4][1] = 6;
     displayMap();
     Wait(3000);
}
When the map is displayed for the second time, the values that I changed in main do not appear. What am I doing wrong? I'm totally stumped. I have tried changing the values in the initMap function, the displayMap function, it's own function, in main and nothing works...any help is appreciated.

Also, I have tried to initialize the map array in a different way (this is the way I want would eventually prefer to do it) but I'm still getting the exact same error where every value is 0...

Code: Select all

void initializeMap()
{
     int j; int k;
     for(j=0; j < SIZE_X; j++)
     {
          map[j][0] = 1;
          map[j][SIZE_Y-1] = 1;
     }
     for(k=0; k < SIZE_Y; k++)
     {
          map[0][k] = 1;
          map[SIZE_X-1][k] = 1;
     }
     for(j=1; j < SIZE_X-1; j++)
     {
          for(k=1; k < SIZE_Y-1; k++)
          {
               map[j][k] = 0;
          }
     }
}
Again, I'm sure it's a rather mundane error so if someone could point it out to my I would be forever indebted to you :)
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: NXC 2D Arrays

Post by m-goldberg »

NXC has only dynamic arrays, so its best to declare arrays without fixed sizes. Also, I don't think you were using ArrayInit correctly. I made a few changes to your code to make conform to my ideas about how arrays should be declared and initialized and the modified appears to work. At the first display it shows all zeros. At the second display it shows 9, 8, 7,6 along the diagonal.

Code: Select all

#define ROWS 7
#define COLS 6

int row[];
int map[][];

void initMap()
{
   ArrayInit(row, 0, COLS);
   ArrayInit(map, row, ROWS);
}

void displayMap()
{
   int counter = 0;
   int value;
   int i;

   ClearScreen();
   for(i=0; i < ROWS; i++)
   {
      value = map[i][5];
      NumOut(counter, LCD_LINE1, value);
      counter += 10;
   }
   counter = 0;
   for(i=0; i < ROWS; i++)
   {
      value = map[i][4];
      NumOut(counter, LCD_LINE2, value);
      counter += 10;
   }
   counter = 0;
   for(i=0; i < ROWS; i++)
   {
      value = map[i][3];
      NumOut(counter, LCD_LINE3, value);
      counter += 10;
   }
   counter = 0;
   for(i=0; i < ROWS; i++)
   {
      value = map[i][2];
      NumOut(counter, LCD_LINE4, value);
      counter += 10;
   }
   counter = 0;
   for(i=0; i < ROWS; i++)
   {
      value = map[i][1];
      NumOut(counter, LCD_LINE5, value);
      counter += 10;
   }
   counter = 0;
   for(i=0; i < ROWS; i++)
   {
      value = map[i][0];
      NumOut(counter, LCD_LINE6, value);
      counter += 10;
   }
}

task main()
{
   initMap();
   displayMap();
   Wait(SEC_5);
   map[1][4] = 9;
   map[2][3] = 8;
   map[3][2] = 7;
   map[4][1] = 6;
   displayMap();
   while (true);
}
Regards, Morton
skelly89
Posts: 10
Joined: 13 Oct 2010, 21:41

Re: NXC 2D Arrays

Post by skelly89 »

Welp, I feel like a doofus. After trying out your code and still having the same issue, I had totally forgotten I had didnt' have the correct firmware on my Brick, which did not support multi-dimensional arrays. Downloading the correct version solved all my problems, although your code example was more efficient than mine. Thank you very much :)
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests