Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Two-dimensional Arrays
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Witchcraft




PostPosted: Tue Nov 11, 2008 5:52 pm   Post subject: Two-dimensional Arrays

As a test of my programming skills, I've decided to make tetris in Turing. Obvioulsy each row of boxes needs to be an array of 1..10, however instead of making 20 of these variables (the columns are 20 rows high). I thought of making an array of an array and it worked, I didn't get an error. My code looks like this:

var block : array 1 .. 20 of array 1 .. 10 of boolean

After doing a little research I was led to believe that this is called a two-dimensional array (correct me if I'm wrong). So I'm assuming that because I got no error on that line of code, that I declared it correctly, however I dont know how to call an element from this array (didn't find anything in the help). If anybody knows, could they please tell me!
Thanks

P.S. I'm new to programming forums, so could someone tell me how to make my code appear as it would in turing?
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Tue Nov 11, 2008 6:19 pm   Post subject: Re: Two-dimensional Arrays

If I remember correctly, 2 demensional arrays are created by saying
Turing:

var myarray : array 1 .. 20, 1 .. 20 of int


which creats an array of 20 elements each with 20 elements of their own. while an array of an array could probably work(I dont actually know, never been inclined to try it), it will be much harder than simply defining a 2D array in the method i used.

to access elements in a 2D array, simply use

Turing:

myarray(2,20) := 4

so you would access the 20th object under the 2nd element.

also, to create Turing syntax boxes like the ones i've used, simply use

code:

[syntax="Turing"]
%your code goes in here
[/syntax]
Witchcraft




PostPosted: Tue Nov 11, 2008 8:44 pm   Post subject: RE:Two-dimensional Arrays

thanks a lot, I havn't got a change to try it out yet, but you seem pretty confident so I'll take your word for it
Insectoid




PostPosted: Tue Nov 11, 2008 8:48 pm   Post subject: RE:Two-dimensional Arrays

Just a note: I recently discovered that the second array in a 2D array cannot take a variable as it's size. Very annoying if you want it to represent the pixels in a picture. For example:

Turing:

var pixel: array 1..picWidth of array 1..picHeight of int


This won't work.

And, on older versions of Turing (4.0.5 at least), arrays cannot be declared the way TheGaurdian posted, and must be refered to as
code:

foo (x)(y)
%rather than
foo(x,y)
[Gandalf]




PostPosted: Tue Nov 11, 2008 8:59 pm   Post subject: Re: RE:Two-dimensional Arrays

insectoid @ 2008-11-11, 8:48 pm wrote:
on older versions of Turing (4.0.5 at least), arrays cannot be declared the way TheGaurdian posted

Actually, the can, except only one of the array's dimensions may be dynamically sized. So picWidth would have to be replaced with an integer literal.

Edit: Err, I got a bit confused as to what you were referring, however my post remains true. Arrays may be declared as TheGuardian posted. If you declare them like you propose, only the last dimension may be dynamically sized.
Insectoid




PostPosted: Tue Nov 11, 2008 9:02 pm   Post subject: RE:Two-dimensional Arrays

Is limited to older versions, or are new(relatively) versions restricted by this to?(picWidth being derived from Pic.Width())
[Gandalf]




PostPosted: Tue Nov 11, 2008 9:05 pm   Post subject: RE:Two-dimensional Arrays

Yes, that's why you really should be using the array 1 .. m, 1 .. n of type syntax.
Insectoid




PostPosted: Tue Nov 11, 2008 9:10 pm   Post subject: RE:Two-dimensional Arrays

Sounds good, fixed one problem that myself and my friend both have had (both of our mini-projects involve getting all the components of a picture and saving to an array)
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: