Posted: Tue Apr 18, 2006 9:38 am Post subject: Three Dimensional Array
Can someone give me a three dimensional array tutorial ppplllzzzzzzzzzz ^^
Sponsor Sponsor
DIIST
Posted: Tue Apr 18, 2006 9:47 am Post subject: (No subject)
code:
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! Its just like two dimensional arrays, execpt you declare a new dimension.
Delos
Posted: Tue Apr 18, 2006 1:44 pm Post subject: (No subject)
If you want to request a Tutorial, please post here. Do not start new topics. This thread has hence been moved to [Turing Help].
If you want a multi-dim array tut, look 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.
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]
Posted: Tue Apr 18, 2006 2:59 pm Post subject: (No subject)
Delos wrote:
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:
code:
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
Posted: Tue Apr 18, 2006 4:27 pm Post subject: (No subject)
what about 4.0.4c?
Delos
Posted: Tue Apr 18, 2006 4:48 pm Post subject: (No subject)
[Gandalf] wrote:
Delos wrote:
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:
code:
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
Posted: Tue Apr 18, 2006 8:32 pm Post subject: (No subject)
TokenHerbz wrote:
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
Posted: Wed Apr 19, 2006 12:51 am Post subject: (No subject)
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.
Sponsor Sponsor
do_pete
Posted: Wed Apr 19, 2006 7:40 am Post subject: (No subject)
Just out of curiousity, why is it that hard for the guys at Holtsoft to make multi-dimensional flexible arrays?
NikG
Posted: Wed Apr 19, 2006 9:30 am Post subject: (No subject)
do_pete wrote:
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
Posted: Wed Apr 19, 2006 8:59 pm Post subject: (No subject)
do_pete wrote:
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
Posted: Wed Apr 19, 2006 10:19 pm Post subject: (No subject)
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
Posted: Mon May 08, 2006 9:55 am Post subject: (No subject)
thx guys ^^ now i have another question about three dimensional array ... if you have an array for example
code:
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
code:
var list: array 1 ..5 of int :=init (2,3,4,5,6)
how do u do the same thing for three dimensional array
Delos
Posted: Mon May 08, 2006 10:21 am Post subject: (No subject)
Well, you have a 2-dim array there! Anyhow, your best bet would be to use a series of nested for loops:
pseudo:
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:
pseudo:
the_array (1, 1) := ...
the_array (2, 1) := ...
or if you have to use init:
code:
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
Posted: Tue May 09, 2006 9:13 pm Post subject: (No subject)
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 ^^