2D Arrays, or Something?
Author |
Message |
[Gandalf]
|
Posted: Mon Jun 20, 2005 4:21 pm Post subject: 2D Arrays, or Something? |
|
|
I'm just a bit confusled right now, having an exam and the sort tomorrow.
Let's say I had this:
code: | var bullet : flexible array 1 .. 0 of
record
x, y
end record |
How would I add a say, bullet(2) so that I don't have to copy the whole thing and rename the new bullet to bullet2. So pretty much what I'm asking is, how would I shorten something like this:
code: | var bullet : flexible array 1 .. 0 of
record
x, y : real
end record
var bullet2 : flexible array 1 .. 0 of
record
x, y : real
end record |
Thanks. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Bacchus
|
Posted: Mon Jun 20, 2005 4:39 pm Post subject: (No subject) |
|
|
Ok, now I'm a bit confusled about what you mean. Do you mean like a shorter way of making another array with the same record?
Turing: | type positions :
record
x, y : real
end record
var bullet : flexible array 1 .. 0 of positions
var bullet2 : flexible array 1 .. 0 of positions | Or do you mean adding another bullet to your array?
Turing: | var bullet : flexible array 1 .. 0 of
record
x, y : real
end record
new bullet, 2 | Or do you mean like adding another dimension to the flexible array? Please explain a bit more. |
|
|
|
|
|
lyam_kaskade
|
Posted: Mon Jun 20, 2005 4:40 pm Post subject: (No subject) |
|
|
code: |
type foo :
record
x, y : int
end record
var bullet : flexible array 1 .. 0 of foo
var bullet2 : flexible array 1 .. 0 of foo
| [/code] |
|
|
|
|
|
Cervantes
|
Posted: Mon Jun 20, 2005 4:41 pm Post subject: (No subject) |
|
|
Well, you can make it into a type and then create different variables:
code: |
type obj :
record
x, y, : real
end record
var bullet : flexible array 1 .. 0 of obj
var bullet2 : flexible array 1 .. 0 of obj
|
But if you've got a flexible array of bullets, why are you making bullet and bullet2? In any case, if you must do that, you could try a 2D array.
code: |
type obj :
record
x, y : real
end record
var bullet : flexible array 1 .. 0, 1 .. 0 of obj
|
EDIT: 2 posts got there before me. Yee-gawds! |
|
|
|
|
|
[Gandalf]
|
Posted: Mon Jun 20, 2005 5:05 pm Post subject: (No subject) |
|
|
Ya, thanks - I was forgetting about types
Quote: But if you've got a flexible array of bullets, why are you making bullet and bullet2?
Well, they're not two different bullets, they're two kinds of bullets going in different directions. |
|
|
|
|
|
MysticVegeta
|
Posted: Mon Jun 20, 2005 6:35 pm Post subject: (No subject) |
|
|
doh! there can be a flexible array with mutiple dimension, jeez and all this time i have been sleeping |
|
|
|
|
|
Cervantes
|
Posted: Mon Jun 20, 2005 6:53 pm Post subject: (No subject) |
|
|
I was pressured into adding that to the tutorial. Check it out |
|
|
|
|
|
Bacchus
|
Posted: Mon Jun 20, 2005 9:38 pm Post subject: (No subject) |
|
|
Even though in your Tutorial Cervantes, you list some basic rules... Like Quote: In a 2D array, if you leave one upper bounds alone, you can do whatever you want to the other bounds. That doesn't work Try it out:
code: | var foo:flexible array 1..3,1..2 of int
new foo,3,3 | There is a bit of a way around this, takes up a bit of space thought. Just create a new array (doesn't have to be flexible) and store the orginal array in that then change the dimension to 0 and back to what you want before restoring the original array's values. lol |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Tue Jun 21, 2005 9:56 am Post subject: (No subject) |
|
|
Ooh, thanks for that, Bacchus. It's updated. |
|
|
|
|
|
MysticVegeta
|
Posted: Tue Jun 21, 2005 11:05 am Post subject: (No subject) |
|
|
Bacchus wrote: Just create a new array (doesn't have to be flexible) and store the orginal array in that then change the dimension to 0 and back to what you want before restoring the original array's values. lol
i see i see, pretty nifty.I think the reason they cant make it 3, 3 is because turing doesnt know the flexiblility of the elements of each of the dimensions of the flexible array. |
|
|
|
|
|
Cervantes
|
Posted: Tue Jun 21, 2005 1:07 pm Post subject: (No subject) |
|
|
Bah! This is not so pretty!
Turing: |
var list : flexible array 1 .. 3, 1 .. 2 of int
for i : 1 .. upper (list, 1)
for j : 1 .. upper (list, 2)
list (i, j ) := i * j
end for
end for
var tempArr : array 1 .. upper (list, 1), 1 .. 4 of int
for i : 1 .. upper (tempArr, 1)
for j : 1 .. upper (tempArr, 2)
if i <= upper (list, 1) & j <= upper (list, 2) then
tempArr (i, j ) := list (i, j )
else
tempArr (i, j ) := minint %flag
end if
end for
end for
new list, upper (list, 1), 0
new list, upper (list, 1), upper (tempArr, 2)
for i : 1 .. upper (list, 1)
for j : 1 .. upper (list, 2)
%if tempArr (i, j) ~= minint then
list (i, j ) := tempArr (i, j )
%end if
end for
end for
for i : 1 .. upper (list, 1)
for j : 1 .. upper (list, 2)
put list (i, j )
end for
end for
|
I wanted to make a procedure of it, but it's a syntax error to do this:
code: |
proc foo (var arr : flexible array 1 .. *, 1 .. * of int)
|
Gandalf, my apologies for taking over your thread. I'll leave if you want. |
|
|
|
|
|
[Gandalf]
|
Posted: Tue Jun 21, 2005 1:19 pm Post subject: (No subject) |
|
|
No, it's alright . You always learn new things, and these kinds of discussions are useful. Trying to do what Turing cannot accept, using Turing . Maybe you'll even come up with something for Stargates project.
Besides, its nice to have a long topic! |
|
|
|
|
|
|
|