Jump to page: 1 2 3
Thread overview
Doubt - Static multidimension arrays
Jan 19, 2016
albert
Jan 19, 2016
cym13
Jan 19, 2016
albert00
Jan 19, 2016
albert00
Jan 19, 2016
tsbockman
Jan 19, 2016
albert00
Jan 19, 2016
tsbockman
Jan 19, 2016
tsbockman
Jan 19, 2016
tsbockman
Jan 19, 2016
Mike Parker
Jan 19, 2016
tsbockman
Jan 19, 2016
Mike Parker
Jan 19, 2016
tsbockman
Jan 19, 2016
Mike Parker
Jan 19, 2016
Mike Parker
Jan 19, 2016
alb
Jan 19, 2016
tsbockman
Jan 21, 2016
Nemo
Jan 21, 2016
tsbockman
Jan 19, 2016
Ali Çehreli
Jan 19, 2016
Albert00
Jan 19, 2016
Ali Çehreli
Jan 19, 2016
alb
January 19, 2016
Hello,

I was looking over http://dlang.org/spec/arrays.html#rectangular-arrays:

And I've found that D multidimensional arrays are Rectangular, so it's impossible to create non-rectangular multidimensional static array like:

int[2][5] arr;

?
January 19, 2016
On Tuesday, 19 January 2016 at 02:47:04 UTC, albert wrote:
> Hello,
>
> I was looking over http://dlang.org/spec/arrays.html#rectangular-arrays:
>
> And I've found that D multidimensional arrays are Rectangular, so it's impossible to create non-rectangular multidimensional static array like:
>
> int[2][5] arr;
>
> ?

How is that not rectangular? It's sounds like you're confusing it with "square".
January 19, 2016
On Tuesday, 19 January 2016 at 02:54:03 UTC, cym13 wrote:
>> int[2][5] arr;
>>
>> ?
>
> How is that not rectangular? It's sounds like you're confusing it with "square".

Ow my problem is:

int[2][2] arr;  // This works

int[2][5] arr;  // This not working

And I'd like to create the former.


January 19, 2016
Well maybe it was my fault, but anyway, here's a small example of what I was working on:

void main(){
	// Array 1	
	int[2][2] arr1;

	arr1[0][0] = 1;
	arr1[0][1] = 2;
	arr1[1][0] = 3;
	arr1[1][1] = 4;

	// Array 2
	int[1][2] arr2;
	
	//arr2[0][0] = 1;
	//arr2[0][1] = 2; // <- Error
	
	arr2[0][0] = 1;
	arr2[1][0] = 2;
}

So for what I can see on Array 2 is the column/row works otherwise.

It's strange since I was declaring int[1][2] (1 row / 2 columns) and then accessing as:

	arr2[0][0] = 1;
	arr2[1][0] = 2;

Seems like 2 rows and 1 column. This makes sense?
January 19, 2016
On Tuesday, 19 January 2016 at 03:20:30 UTC, albert00 wrote:
> [...]

You're not really creating a rectangular array - what you're making is an array *of arrays*:

int[10][5] a; // An array of 5 (int[10])

writeln(typeof(a).stringof);      // int[10][5]
writeln(typeof(a[4]).stringof);    // int[10]
writeln(typeof(a[4][9]).stringof); // int

The recently accepted `std.experimental.ndslice` module provides a real multi-dimensional array interface:
    http://dlang.org/phobos-prerelease/std_experimental_ndslice.html

(There might be something else relevant in Phobos as well, with a less numerically-oriented focus; I'm not sure.)

January 18, 2016
On 01/18/2016 07:20 PM, albert00 wrote:

> It's strange since I was declaring int[1][2] (1 row / 2 columns) and
> then accessing as:
>
>      arr2[0][0] = 1;
>      arr2[1][0] = 2;
>
> Seems like 2 rows and 1 column. This makes sense?

Yes, it makes sense and its consistent. This is one of many little things that D is an improvement over C and C++.

Static array type syntax is always the following:

    Type[length]

For example, the following is a row with two columns:

    int[2]

When you want a certain number of those, you follow the same array definition syntax:

    Type[length]

Since each element is int[2] in this case, for 3 rows we get:

    int[2][3]

So, in order to get 1 row of 2 columns, you would write

    int[2][1]

Accessing elements is consistent as well:

    var[index]

So,

    arr2[0]

would be the first row, which has two columns; which is further accessed by another index:

    arr2[0][1]

is first row, second column.

Ali

January 19, 2016
On Tuesday, 19 January 2016 at 05:32:07 UTC, Ali Çehreli wrote:

Ali, look what you said:

> For example, the following is a row with two columns:
>
>     int[2]

Then you said:

> So, in order to get 1 row of 2 columns, you would write
>
>     int[2][1]

So the first pair of square-brackets is the column and second is the row as you said above, but look what happens when I try to access thinking that way:

void main(){
	int[2][1] arr; // 2 columns & 1 row as Ali said...
	
	arr[0][0] = 1;
	arr[1][0] = 2;
}

ERROR:

/d609/f167.d(14): Error: array index 1 is out of bounds arr[0 .. 1]
/d609/f167.d(14): Error: array index 1 is out of bounds arr[0 .. 1]


So now the first pair of brackets in fact is the ROW and the second is the COLUMN, because this works:

void main(){
	int[2][1] arr; // 2 columns & 1 row
	
	arr[0][0] = 1;
	arr[0][1] = 2;
}

Maybe I'm really dumb, but you need to agree that even with your good explanation it still doesn't making sense.

Albert.
January 18, 2016
On 01/18/2016 11:12 PM, Albert00 wrote:
> On Tuesday, 19 January 2016 at 05:32:07 UTC, Ali Çehreli wrote:
>
> Ali, look what you said:
>
>> For example, the following is a row with two columns:
>>
>>     int[2]
>
> Then you said:
>
>> So, in order to get 1 row of 2 columns, you would write
>>
>>     int[2][1]

To rephrase myself, that is an array of 1 element, where the element type is int[2]. So the only valid index is 0, which gives you an element of int[2]. (You can further index into that element of course.)

> So the first pair of square-brackets is the column and second is the row
> as you said above,

I stress the fact that it is always the following syntax:

    Type[length]

So, again, if we have an array of 1-element where the elements are of type int[2], then it is this (space added for readability):

    int[2] [1]

> but look what happens when I try to access thinking
> that way:

I suspect C and C++ way for inside-out (or is it outside-in) syntax is affecting your thinking. ;)

> void main(){
>      int[2][1] arr; // 2 columns & 1 row as Ali said...
>
>      arr[0]

That one gives you the first element.

> [0] = 1;

and that one gives you the first element of that first element.

>      arr[1][0] = 2;

Sorry, there is no element-1 for the arr: That has only one element.

> }
>
> ERROR:
>
> /d609/f167.d(14): Error: array index 1 is out of bounds arr[0 .. 1]
> /d609/f167.d(14): Error: array index 1 is out of bounds arr[0 .. 1]
>
>
> So now the first pair of brackets in fact is the ROW and the second is
> the COLUMN, because this works:
>
> void main(){
>      int[2][1] arr; // 2 columns & 1 row

Yes, that's exactly what I said. :)

>      arr[0][0] = 1;
>      arr[0][1] = 2;
> }
>
> Maybe I'm really dumb,

Not at all. I blame C and C++. ;)

> but you need to agree that even with your good explanation it
> still doesn't making sense.

I don't agree: It makes sense and is consistent. :)

> Albert.

Ali

January 19, 2016
On Tuesday, 19 January 2016 at 04:50:18 UTC, tsbockman wrote:
> On Tuesday, 19 January 2016 at 03:20:30 UTC, albert00 wrote:
>> [...]
>
> ... what you're making is an array *of arrays*:

Maybe I was misunderstood, because in fact that is what I was making an array of arrays, but my problem in fact was in accessing it.

Like I said above:

I was declaring int[1][2] arr:

But to access I need to invert the columns like:

	arr2[0][0] = 1;
	arr2[1][0] = 2;


> int[10][5] a; // An array of 5 (int[10])

Using your example, in my head I'd access the last element as:

a[9][4] but that would give me an error:

Error: array index 9 is out of bounds arr[0 .. 5]

So I need to invert a[4][9].

Again seems a bit strange "FOR ME" since I declare in one way and access the other way.

albert.

PS: I'm changing my name because I think there is another user with the same name, that Icon is not mine.
January 19, 2016
On Tuesday, 19 January 2016 at 07:19:54 UTC, Ali Çehreli wrote:
> ...

Well anyway thanks for your help. For now I'll just think the otherwise. :)

Albert.


« First   ‹ Prev
1 2 3