July 28, 2015
On Tuesday, 28 July 2015 at 16:24:39 UTC, anonymous wrote:
> On Tuesday, 28 July 2015 at 16:09:46 UTC, Binarydepth wrote:
>> Here is what I'm trying to do :
>>
>> import std.stdio : readf, writef;
>> void main()	{
>> 	int[2][] nam;
>> 	int num;
>> 	readf(" %d", &num);
>> 	nam.length = num;
>> 	foreach(nim; 0..num)	{
>> 		readf(" %d %d", &nam[0][num], &nam[1][num]);
>> 	}
>> 	foreach(nim; 0..num)	{
>> 		writef(" %d %d\n", &nam[0][num], &nam[1][num]);
>> 	}
>> }
>
>
> In addition to Adam:
> there are typos (num instead of nim) - since num is the array length and the indices are 0-based, num is out of bounds...
>
> 	foreach(nim; 0..num)	{
> 		readf(" %d %d", &nam[nim][0], &nam[nim][1]);
> 	}
> 	foreach(nim; 0..num)	{
> 		writef(" %d %d\n", nam[nim][0], nam[nim][1]);
> 	}
> works fine.

It works with 2 as input but shows error when number is 3 :(
July 28, 2015
On Tuesday, 28 July 2015 at 16:41:40 UTC, Binarydepth wrote:
> It works with 2 as input but shows error when number is 3 :(


I can't reproduce that or I misunderstood something:

$ cat a.d
import std.stdio : readf, writef;
 void main()	{
 	int[2][] nam;
 	int num;
 	readf(" %d", &num);
 	nam.length = num;
	foreach(nim; 0..num)	{
 		readf(" %d %d", &nam[nim][0], &nam[nim][1]);
 	}
 	foreach(nim; 0..num)	{
 		writef(" %d %d\n", nam[nim][0], nam[nim][1]);
 	}
}
$ dmd a.d
$ ./a
3
1 2
3 4
5 6
 1 2
 3 4
 5 6


July 28, 2015
On Tuesday, 28 July 2015 at 16:53:35 UTC, anonymous wrote:
> On Tuesday, 28 July 2015 at 16:41:40 UTC, Binarydepth wrote:
>> It works with 2 as input but shows error when number is 3 :(
>
>
> I can't reproduce that or I misunderstood something:
>
> $ cat a.d
> import std.stdio : readf, writef;
>  void main()	{
>  	int[2][] nam;
>  	int num;
>  	readf(" %d", &num);
>  	nam.length = num;
> 	foreach(nim; 0..num)	{
>  		readf(" %d %d", &nam[nim][0], &nam[nim][1]);
>  	}
>  	foreach(nim; 0..num)	{
>  		writef(" %d %d\n", nam[nim][0], nam[nim][1]);
>  	}
> }
> $ dmd a.d
> $ ./a
> 3
> 1 2
> 3 4
> 5 6
>  1 2
>  3 4
>  5 6

Your code works but I don't understand why this has to go backwards. But well I don't know how "nam.length = num;" works

My confusion comes because I declare "int[2][]" and then I use it backwards
July 28, 2015
On 7/28/15 12:59 PM, Binarydepth wrote:

> My confusion comes because I declare "int[2][]" and then I use it backwards

This can definitely be confusing.

The way array types work in D is that they are of the form Type[]. This means that the element of Type[] is Type. When Type is actually another array, then you have a multi-indexed array.

So in your case, "Type" is "int[2]". This means, each element of the dynamic array is a 2-element fixed-sized array.

When indexing, it always goes out to in. So nam[0] is the first element of type int[2], and nam[0][0] is the first integer in that first element.

Hope this helps. While confusing, it's also VERY consistent and logical.

-Steve
July 28, 2015
On Tuesday, 28 July 2015 at 17:07:47 UTC, Steven Schveighoffer wrote:
> On 7/28/15 12:59 PM, Binarydepth wrote:

> When indexing, it always goes out to in. So nam[0] is the first element of type int[2], and nam[0][0] is the first integer in that first element.

> -Steve

I don't get what you mean here. In general I understood that in D multidimensional arrays are a group of arrays.
July 28, 2015
On 7/28/15 1:26 PM, Binarydepth wrote:
> On Tuesday, 28 July 2015 at 17:07:47 UTC, Steven Schveighoffer wrote:
>> On 7/28/15 12:59 PM, Binarydepth wrote:
>
>> When indexing, it always goes out to in. So nam[0] is the first
>> element of type int[2], and nam[0][0] is the first integer in that
>> first element.
>
>
> I don't get what you mean here. In general I understood that in D
> multidimensional arrays are a group of arrays.

What I mean is the first index operation operates on the entire type. The second one operates on the element that the first retrieved, and so on.

-Steve
July 28, 2015
On 07/28/2015 10:26 AM, Binarydepth wrote:

> In general I understood that in D
> multidimensional arrays are a group of arrays.

The confusion comes from the fact that D does not have multidimensional arrays. (Neither C and nor C++.)

The array syntax is simple.

Definition:

    Type[] name;

Indexing:

    name[i];

Done... :)

Yes, 'Type' can be an array but that does not change anything.

Ali

July 28, 2015
On Tuesday, 28 July 2015 at 17:26:39 UTC, Binarydepth wrote:
> On Tuesday, 28 July 2015 at 17:07:47 UTC, Steven Schveighoffer wrote:
>> On 7/28/15 12:59 PM, Binarydepth wrote:
>
>> When indexing, it always goes out to in. So nam[0] is the first element of type int[2], and nam[0][0] is the first integer in that first element.
>
>> -Steve
>
> I don't get what you mean here. In general I understood that in D multidimensional arrays are a group of arrays.

A bit off-topic.

I've done a lot of image processing work that deals with 2D arrays in particular.  I must admit that for the most part now I just use a 1D array for all my 2D arrays and do a bit of index arithmetic to figure the location in 2D.

To index I have to do something like (for row major order):

my2darray[row * COLS_PER_ROW + col] = blah;

But I find this has many advantages:

1. The tiny bit of arithmetic is more than offset by not having to deal with trying to remember the order of the indices.

2. You can chose if you want row or column major order.

3. It makes lots of operations much easier, ie. adding two images together if they are of the same dimensions, generating histograms, calculating the average value in a 2D array.

4. Easy to set size dynamically.

5. Consistent between pretty much every programming language (ie. my C and D code would look the same).

6. (IMHO) Code is actually more readable.  But then I find lots of brackets confusing ... so maybe its just me.






July 28, 2015
On 7/28/15 3:33 PM, CraigDillabaugh wrote:

> 6. (IMHO) Code is actually more readable.  But then I find lots of
> brackets confusing ... so maybe its just me.

Have you considered a wrapper that uses multi-dimensional access? i.e.:

my2darray[row, col] = blah;

-Steve
July 28, 2015
On Tuesday, 28 July 2015 at 20:07:12 UTC, Steven Schveighoffer wrote:
> On 7/28/15 3:33 PM, CraigDillabaugh wrote:
>
>> 6. (IMHO) Code is actually more readable.  But then I find lots of
>> brackets confusing ... so maybe its just me.
>
> Have you considered a wrapper that uses multi-dimensional access? i.e.:
>
> my2darray[row, col] = blah;
>
> -Steve

It has crossed my mind, but I've got so used to doing the position calculations I haven't yet tried it.  I guess you would simply need to put your array in a struct and overload the '[]' operator correct? (Plus the appropriate constructors, etc).