Arrays
Author |
Message |
azndragon
|
Posted: Sun Jan 05, 2003 3:18 pm Post subject: Arrays |
|
|
I was just wondering what an array is, and what it does. I checked the tutorial that comes with Turing, and read it. Problem is, the examples that they give you is all wrong, so I was wondering if anyone could post a little description of what it does, along with a working example, etc. Thanks. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Sie_Kensou
|
Posted: Sun Jan 05, 2003 4:32 pm Post subject: Arrays |
|
|
An array is a set of variables of the same type. What is so good about arrays is that you can use them along with loops. For example, let's say you want to have two balls bouncing at the same time. You COULD use two different sets of variables such as 'ball1' and 'ball2', the problem comes when the are, say, 100 balls.... imagine assigning values to each (coordinates for example):
ball1X := 0
ball2X := 0
ball3X := 0
...
ball100X = 0
Instead, you can group them all with one variable name (such as 'ballX') and then simply use and index to refer to each:
var ballX : array 1.. 100 of int
for c: 1.. 100
ballX(c) := 0
end for
This is mainly what arrays are useful for... but I guess there are other applications which come in handy as well.
Ps. Talking about grouping, I also find the following type of coding extremly useful in turing (since there are no "classes" as in Java)
type coordinates:
record
x : int
y: int
end record
var ball : array 1 .. 100 of coordinates
Then you can access each item as:
ball('index').x := 0
ball('index').y := 0 |
|
|
|
|
|
DarkHelmet
|
Posted: Sun Jan 12, 2003 1:55 am Post subject: (No subject) |
|
|
You can also have multi dimensional arrays. These are basically an array inside an array. say you want to store 2 coordinates for a set of 5 things, you could use a multidimensional array. They go as following:
var intnum : array 1..5, 1..2 of int
in order to use these you would have to tell it both the numbers in the brackets. The following example would fill this array with numbers from 1 to 10:
for i : 1..5
for x : 1..2
randint(intnum(i, x), 1, 10)
end for
end for |
|
|
|
|
|
|
|