
-----------------------------------
Andy
Thu May 29, 2003 8:33 pm

[Tutorial] Arrays
-----------------------------------
since arrays are essential in programming, i thought i'd make a little tutorial for the newbies

arrays are simply a collection of variables with the same type, to identify them, you'll have to call them up using their subscripts. If u were to store 10 names using variables and to not use arrays, you'll have to declare them one by one:


var names1, names2, names3, names4, names5, names6, names7, names8, names9, names10 :string 


this may not seem too hard, but what if u were to store 100 names instead? (if u were thinking of putting them in one string and indexing it then shut up, and keep it to ur self smart @SS)


var names1, names2, names3, names4, names5, names6, names7, names8, names9, names10 ... names100 :string 


as you can see, this gets very long and boring after a while, so the smart ppl who invented programming languages came up with an idea : allow the user to store data of the same type under one variable: the arrays.
here is the code for creating that 100 names of variable with an array.


var names: array 1..100 of string


where names is the array name, 1 is the starting of the array, 100 is the end of the array, and string is the type of the array. All the variables decalred in an array have to be the same type, whether it is integer, real, string, or boolean (if you know types then shut up smart @SS)

to call up a stored value in the array, you must type the array named, and then the subscript (variable # in the array) in barrackets. to call up the 49th string in the predefined array 


names (49)


arrays, however, cannot be declared like normal variables, to declare an array one must use init


var names: array 1..10 of string:=init("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",)


you cannot skip one variable and go on to the other, this is why init is only used when declaring an array with a few subscripts to declare large number of variables in an array, you can use a for loop


for i:1..100
names(i):=""
end for


the previous code initializes the variables in the array to be nothing. The upper command tells you the higher boundry of an array and the lower command tells you the lower bondry of an array


var names: array 50..100 of string
upper(names)=100
lower(names)=50


Arrays can be multidimentioned, you can have 2D arrays, 3D, arrays 4D arrays, 100D arrays. each dimention allows the user to multiply the original array by the number of sectors on the next dimention(that didn't make much sense). In short, multidementional arrays are just arrays of arrays. here is an example


var grid:array 1..10,1..10 of int


this declares grid as a two dimentional array allowing it to store 100 variables. there are 10 sectors in every one of the first set of range

you can also change the upper boundry of the array, but not the lower ones, you have to declare them as flexible arrays


var name : flexible array 1..10 of string


this tells the computer that the array is flexible. to change the the upper boundry of the array you have to use the 'new' command, the following code chages the upper of the array to 100


new name , 100


to sort an list of arrays, you simply compare each one with another


for i:1..upper(names)-1
for j:i+1,upper(names)
if names(j)