Computer Science Canada

init commands for multidimensional arrays

Author:  Lucas [ Sun Apr 03, 2011 6:49 pm ]
Post subject:  init commands for multidimensional arrays

What is it you are trying to achieve?
Just trying to use an "init" command for multidimensional arrays to save space, cant figure how to do it though. is it doable?

Author:  Tony [ Sun Apr 03, 2011 7:16 pm ]
Post subject:  RE:init commands for multidimensional arrays

Turing Docs to the rescue! init
code:

var names : array 1 .. 2, 1 .. 3 of string :=
            init ( "Tom", "Dick", "Harry",
                 "Alice", "Barbara", "Cathy")

Author:  Raknarg [ Mon Apr 04, 2011 8:20 am ]
Post subject:  RE:init commands for multidimensional arrays

Its the same as a regular array, you input initial values for the amount of elements you have.

Author:  Lucas [ Mon Apr 04, 2011 6:10 pm ]
Post subject:  RE:init commands for multidimensional arrays

aha k thx, thats what i had though but when i tried it it didn,t work, wrong variable type because i had declared my variables as
var names : array 1 .. 2 of array 1 .. 3 of string :=
init ( "Tom", "Dick", "Harry",
"Alice", "Barbara", "Cathy")

Author:  Raknarg [ Mon Apr 04, 2011 7:46 pm ]
Post subject:  RE:init commands for multidimensional arrays

Yeah, remember when we were looking at my program, you suggested using multi d arrays? Remember how it didn't work because you needed a comma? Razz

Author:  Tony [ Mon Apr 04, 2011 7:54 pm ]
Post subject:  RE:init commands for multidimensional arrays

both are valid, but work subtly differently, and require different syntax
code:

var foo : array 1..1, 1..1 of int
foo(1,1)

vs
code:

var foo : array 1..1 of array 1..1 of int
foo(1)(1)

The latter doesn't get the benefit of init, but you could select an entire row at a time; foo(1) has "array of int" type.

Edit: not sure about the validity of those indices.


: