
-----------------------------------
wingless_angel
Tue Apr 18, 2006 9:38 am

Three Dimensional Array
-----------------------------------
Can someone give me a three dimensional array tutorial :D ppplllzzzzzzzzzz ^^

-----------------------------------
DIIST
Tue Apr 18, 2006 9:47 am


-----------------------------------

var num:array 1..5,1..5,1..5 of int

num(1,1,1):=5

put num (1,1,1)



How hard is that! 8-)  Its just like two dimensional arrays, execpt you declare a new dimension.

-----------------------------------
Delos
Tue Apr 18, 2006 1:44 pm


-----------------------------------
If you want to request a Tutorial, please post [url=http://www.compsci.ca/v2/viewtopic.php?t=3523]here.  Do not start new topics.  This thread has hence been moved to [Turing Help].

If you want a multi-dim array tut, look [url=http://www.compsci.ca/v2/viewtopic.php?t=366]here for the basic Array tut and scroll down a bit.  Towards the end Tony's given some advice on them.  By the time you've figured out 2D arrays, moving up to 3D is not much of a stretch.
[url=http://www.compsci.ca/v2/viewtopic.php?t=6723]This tut will give you some info on Flexy arrays - a little more useful, but less applicable in multi-dim with all but the latest version of Turing.

I have to ask - why do you need a 3D array?  It's quite rare to go past 2D arrays - if you need more than one item per index of the array, use types and records.  Check the [Turing Walkthrough] for more details.

-----------------------------------
[Gandalf]
Tue Apr 18, 2006 2:59 pm


-----------------------------------
Flexy arrays - a little more useful, but less applicable in multi-dim with all but the latest version of Turing.
Not even...  As far as I can tell, Turing 4.1 doesn't fix the multidimensional flexible array resizing problem.  For example:
var myArray : flexible array 1 .. 0, 1 .. 0 of int
new myArray, 1, 1
new myArray, 1, 2
Will result in the same error in both Turing 4.0.5 and Turing 4.1.

-----------------------------------
TokenHerbz
Tue Apr 18, 2006 4:27 pm


-----------------------------------
what about 4.0.4c?

-----------------------------------
Delos
Tue Apr 18, 2006 4:48 pm


-----------------------------------
"]Flexy arrays - a little more useful, but less applicable in multi-dim with all but the latest version of Turing.
Not even...  As far as I can tell, Turing 4.1 doesn't fix the multidimensional flexible array resizing problem.  For example:
var myArray : flexible array 1 .. 0, 1 .. 0 of int
new myArray, 1, 1
new myArray, 1, 2
Will result in the same error in both Turing 4.0.5 and Turing 4.1.

No?  Ah well.  Still, flexies are pretty awsome even with a restricted nth bound.

-----------------------------------
Cervantes
Tue Apr 18, 2006 8:32 pm


-----------------------------------
what about 4.0.4c?
If it's not in either of the two versions after 4.0.4c, do you think it would be in 4.0.4c? Granted, Holtsoft does make some pretty unpredictable moves in their releases. 

But still. The answer is no.

Why are you asking, though? Wouldn't it be easier to just paste [Gandalf]'s code that is one post above yours into your 4.0.4c version of Turing and see whether it crashes? Less spam, please and thank you.

-----------------------------------
Martin
Wed Apr 19, 2006 12:51 am


-----------------------------------
If you don't understand 3d arrays it's because you don't understand 2d arrays. There are lots of 2d array tutorials and examples on the site - search through the forums.

-----------------------------------
do_pete
Wed Apr 19, 2006 7:40 am


-----------------------------------
Just out of curiousity, why is it that hard for the guys at Holtsoft to make multi-dimensional flexible arrays?

-----------------------------------
NikG
Wed Apr 19, 2006 9:30 am


-----------------------------------
Just out of curiousity, why is it that hard for the guys at Holtsoft to make multi-dimensional flexible arrays?
Do you think it could possibly have something to do with the flexi-arrays retaining their previous values when they are resized?
I can't really see why this would complicate it since they managed it for one-dimensional arrays (a feature I like a lot, btw), but what do I know...

-----------------------------------
Martin
Wed Apr 19, 2006 8:59 pm


-----------------------------------
Just out of curiousity, why is it that hard for the guys at Holtsoft to make multi-dimensional flexible arrays?

I think the big problem with multi-dimensional flexible arrays is that they'd be really really slow, especially with big arrays.

For example, a 3x3 array looks as follows

[a][b][c]
[d][e][f]
[g][h][i]

It's stored in memory as follows

[a][b][c][d][e][f][g][h][i]

Now, you want to make it 4x3, no problem, just tack a row onto the end:
[a][b][c]
[d][e][f]
[g][h][i]
[j][k][l]

Or, in memory:

[a][b][c][d][e][f][g][h][i][j][k][l]

However, you decide that you want to make it 3x4 (ie. add another column)
[a][b][c][j]
[d][e][f][k]
[g][h][i][l]

In memory:
[a][b][c][j][d][e][f][k][g][h][i][l]

So the steps you have to take are as follows:
1. shift values 4 - the end right 1 memory location:
[a][b][c][ ][d][e][f][g][h][i]
2. Add [j] to the new empty location.
[a][b][c][j][d][e][f][g][h][i]
3. Shift values 7 - the end right 1 memory location:
[a][b][c][j][d][e][f][ ][g][h][i]
4. Add [k] to the new empty location
[a][b][c][j][d][e][f][k][g][h][i]
5. Add [l] to the end
[a][b][c][j][d][e][f][k][g][h][i][l]

It's doable, but it would be very slow and inefficient - especially with larger arrays.

If you need a bigger flexible array, use a queue or a stack instead.

-----------------------------------
NikG
Wed Apr 19, 2006 10:19 pm


-----------------------------------
Great explanation Martin.
I guess I was on the right track about the reason; I just didn't think about it in terms of memory.

-----------------------------------
wingless_angel
Mon May 08, 2006 9:55 am


-----------------------------------
thx guys ^^ now i have another question about three dimensional array ... if you have an array for example 
 var airfare: array 1 ..4, 1..4 of int

then how do you intialize it for three dimensional because I only noe how to do one dimesional for example 
 var list: array 1 ..5 of int :=init (2,3,4,5,6)
how do u do the same thing for three dimensional array :shock:

-----------------------------------
Delos
Mon May 08, 2006 10:21 am


-----------------------------------
Well, you have a 2-dim array there!  Anyhow, your best bet would be to use a series of nested for loops:

for i : 1 .. upper (the_array)
  for j : 1 .. upper (the_array, 2)
     %...
        the_array (i, j, ...) := ...
%...


If you need to manually initialize a 2-dim array, then you could use:

the_array (1, 1) := ...
the_array (2, 1) := ...


or if you have to use init:


var the_array : array 1..2, 1..2 of int := init (1, 2, 3, 4)
% Is the same as:
% 1  3
% 2  4
% Sort of like "(1, 2); (3, 4)"


I've never had to initialize a 3-dim array like that, but I'm assuming it would follow something similar.
However, that being said...I'll reitierate what I said all those weeks back:  why are you using a 3-dim array?  I can't even begin to imagine a case where a 3-dim array would be a better route than using a single-dim array with records instead.  Unless you have a very specific need...?  Please expand a little, I'm quite curious.

-----------------------------------
wingless_angel
Tue May 09, 2006 9:13 pm


-----------------------------------
well let just say i need this for a comp sci project ... and we need to do something special that we didn't learn in class ... and i guess this is wa me n my parneter came up with. Its quite stupid i noe but then thx for the help. Also which of the three ways that you stated above about initalizing will be the best if we know the information b4 hand ? thx a bunch ^^

-----------------------------------
Delos
Wed May 10, 2006 1:12 pm


-----------------------------------
If you know it before hand, and you want to wow your teacher a bit, then do the following:
- enter the info into a file in a well constructed manner.  For instance:
(1, 1, 1);
(2, 3, 4);
(1, 5, 6);
...
- read the files contents into your programme, and use them to initialize your 
3-dim array.

This would be impressive since it would show your understanding of file systems, dynamic arrays, and the power of for loops.

If  you don't want to delve too deeply into that, then either method 2 or 3.  Method 1 would be great if you had a formula to calculate each value (it's also the same method used in the file-reading-method).
