Computer Science Canada

Initializing a two/three dimensional array.

Author:  JayLo [ Tue Apr 15, 2003 5:02 pm ]
Post subject:  Initializing a two/three dimensional array.

how do you initialize values for a two dimensional array?
I know that arrays would be:
var x : array 1 .. 3 of int := init (3,5,7)
but what about 2d arrays?

Author:  Blade [ Tue Apr 15, 2003 5:15 pm ]
Post subject: 

heres the example from help
Quote:
Example
var mensNames : array 1 .. 3 of string :=
init ( "Tom", "Dick", "Harry" )
put mensNames ( 2 ) % This outputs Dick
var names : array 1 .. 2, 1 .. 3 of string :=
init ( "Tom", "Dick", "Harry",
"Alice", "Barbara", "Cathy")
put names ( 2, 1 ) % This outputs Alice


it should explain it for you

Author:  Tony [ Tue Apr 15, 2003 5:19 pm ]
Post subject: 

once again, read the tutorial on the arrays. It covers EVERYTHING you need to know, including explanation of multidimentional arrays.

Author:  Blade [ Tue Apr 15, 2003 5:34 pm ]
Post subject: 

i read it and it didnt tell me how to initilize 2d arrays...

Author:  Tony [ Tue Apr 15, 2003 5:53 pm ]
Post subject: 

read the tutorial again Evil or Very Mad

Quote:
Some array tricks - 2D arrays
Here's something that I used to scare people with right before grade10 compsci exam... "you need to know 2D arrays". People were freaking out... I donno why

Anyway, the point is that each of our little "boxes" in an array can contain a row of boxes of its own. So for each box in a row, we get another row, forming columns. This way we just created a grid (also called matrix).

it's decleared as
Code:

var grid:array 1..10, 1..5 of int



10 rows, 5 columns. Though that depends how you look at it and how you set up your forloops. I had a huge argument with my teacher of what value gous first - row or column. I dont remember what she said, but in my opinion it doesnt matter - as long as you know

Author:  Catalyst [ Tue Apr 15, 2003 6:07 pm ]
Post subject: 

i think he means using the init command...

Author:  Tony [ Tue Apr 15, 2003 6:53 pm ]
Post subject: 

ah crap.

Blade is right then.

though why initialize arrays? just get info from a file Wink

Author:  Blade [ Tue Apr 15, 2003 9:21 pm ]
Post subject: 

YAY BLADE IS RIGHT!! go blade go, oh yeah uh huh! Very Happy hehe i wouldnt have argued with you if i didnt know i was right tony Wink

Author:  JayLo [ Wed May 07, 2003 6:16 pm ]
Post subject: 

about initializing from a text file...
how? Embarassed

Author:  Tony [ Wed May 07, 2003 6:43 pm ]
Post subject: 

:= init(variable1, variable2, ... , variableN)

or just read from the file and save values right in the array

Author:  Homer_simpson [ Wed May 07, 2003 7:11 pm ]
Post subject: 

my new tetris project uses 2d arrays that might help u if u take a look at it....


: