Two dimensional arrays

Hello ARDUBOY community!

I am stuck trying to develop a new game as I can’t get two dimensional arrays working. Trying to define a two dimensional array that is 15 by 7 with a random value for each x,y coordinate.

int ter[15][7];

for (int x = 1; x < 16; x++)
{
  for(int y = 1; y < 8 ; y++)
  {
    int r = rand() % 9 + 1;
    ter[x][y] = r;
  }
}

When I try to use this in and “if” statement it doesn’t seems to have any effect. Also if I try to print ter[x ][y] my ARDUBOY crashes! Can anyone give me some assistance on two dimensional arrays?

weird, apparently you can’t put an x in square brackets or it creates a checkmark

Welcome :slight_smile:

In C/C++, arrays are zero-indexed, so the first element of an N-element array is at index 0 and the last element is at index N-1.

In this case, for the 2D array ter[15][7], try adjusting your for loops to range over 0 to 14 and 0 to 6 instead of 1 to 15 and 1 to 7:

int ter[15][7];

for (int x = 0; x < 15; x++)
{
  for(int y = 0; y < 7 ; y++)
  {
    int r = rand() % 9 + 1;
    ter[x][y] = r;
  }
}

BTW, to paste code blocks, surround them with three ticks:

```cpp
[paste code here]
```

3 Likes

In c++ arrays are usually thought of as array[row][column] or array[y][x]. I understand that this is conceptual but if you find articles or tutorials they will often follow this convention and you might get confused if you go against it.

Thus your code might be better expressed as:

int ter[7][15];

for (int x = 0; x < 15; x++)
{
  for(int y = 0; y < 7 ; y++)
  {
    int r = rand() % 9 + 1;
    ter[y][x] = r;
  }
}
4 Likes

One of the big reasons [y][x]/[row][column] is the standard convention is because it matches the how initialisation works:

int ter[7][15]
{
	// 7 rows of 15 columns
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
};

If you use [x][y]/[column][row] then you have to rotate your data.


One more thing to point out:

It’s actually more efficient to have the outermost loop iterate the leftmost extent of the array and to have the innermost loop iterate the rightmost extent of the array because of the way C++ lays out its arrays in memory.

In this case:

int ter[7][15];

for(int y = 0; y < 7 ; y++)
{
  for (int x = 0; x < 15; x++)
  {
    int r = rand() % 9 + 1;
    ter[y][x] = r;
  }
}

The compiler can optimise that much better because the loops are now going through the array in address order, one element after another, rather than having to leap over multiple elements.

4 Likes

There’s likely something else going on, but nobody could say for definite without seeing your code.

On some platforms, such as desktop, reading outside the bounds of an array could potentially cause a program to crash, but on Arduboy that’s not likely to happen. (Unless perhaps you read so far outside the bounds of an array that you try to read memory that doesn’t exist, but I’m not certain about that.)


When you say ‘crash’, do you mean it resets or it ‘freezes’?

When the Arduboy ‘crashes’, it should just reset.

If your program appears to be ‘freezing’ then that’s likely a different problem, like a never-ending loop, or perhaps your input handling code just isn’t working properly for some reason.

1 Like

It was resetting. Actually I found the bug. Was inputting too high a number into the array by mistake then trying to print it.

I could actually use a little help on another problem. Seems simple enough. I just want to use an "if: statement to skip a section of code. I’m an old BASIC programmer so I’m not sure if it’s just a syntax error.

"
if (o = 1)
{
arduboy.drawLine(10,10,40,40);
}
"

All I want to di is if o = 1 draw this line, or else do not draw the line. Can you see what I’m doing wrong? I know it’s a simple problem but I’m really stuck on this one. any help would be great!

Try:

if (o == 1) {
   arduboy.drawLine(10,10,40,40);
}

In C++, = is an assignment whereas == is a comparison. It will assign the value of 1 to o and (as this is successful, will draw the line every time.

2 Likes

By ‘inputting’ do you mean indexing the array with too large an index or trying to write a value that’s too large for the array’s datatype?

Indexing:

array[y][really_large_number] = whatever;

Storing:

array[y][x] = really_large_number;

I’m presuming the former, as that could theoretically cause a crash by trying to make the Arduboy read nonexistant memory.

The latter shouldn’t cause a crash under normal circumstances.
If it does then there’s almost certainly something else going on.

What @filmote says is correct, = is an assignment operator and == is a comparison operator (more specifically the ‘equality operator’).

To get syntax higlighting on the forum you need to use grave accents, not quote marks. (Some people call them ‘backticks’, but ‘grave accent’ is the proper Unicode name.)

On most UK and US keyboard layouts the ` key can be found above the Tab key, but for other countries it may vary somewhat.

Alternatively you can use old-fashioned BB code:

[code]
if (o == 1) {}
[/code]

if (o == 1) {}

But the downside to that is that you can’t specify the language, whereas grave accents allow the language to be specified.

```cpp
if (o == 1) {}
```

if (o == 1) {}

(Notice the if is highlighted in the grave accent version, but not the BB code version.)

BB code also doesn’t work inline, whereas grave accents do.

1 Like

Yes, that’s exactly what I meant! So it’s all good now, was adding too much to one particular variable!

Thanks so much, yeah how could I forget such a simple thing “==” not “=”? Been a while since I programmed and I got a little rusty!

Yeah, you got it! Such a fundamental thing, I’d just forgot about. Think I’m good now, thanks!

You’re a lifesaver, was stuck on that for a while!