Computer Science Canada

[Tutorial] Arrays

Author:  Andy [ Thu May 29, 2003 8:33 pm ]
Post subject:  [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:

code:

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)

code:

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.

code:

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

code:

names (49)


arrays, however, cannot be declared like normal variables, to declare an array one must use init

code:

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

code:

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

code:

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

code:

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

code:

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

code:

new name , 100


to sort an list of arrays, you simply compare each one with another

code:

for i:1..upper(names)-1
for j:i+1,upper(names)
if names(j)<names(i) then
temp:=names(j)
names(j):=names(i)
names(i):=temp
end if
end for
end for


this code sorts the names array in alphabetical order

well this is about it, if you read all of this, you should be pretty familiar with arrays. if you have any troubles, just pm me 8)

Author:  Andy [ Fri May 30, 2003 3:08 pm ]
Post subject: 

dammit, tony made a tutorial on arrays too, but anyways, my is longer, better, and easier to understand rite guys? hey wait a second, how come there are no replies in this post, damn you @SS sucking ppl,
AHHHH i'm just kidding, no Asok you psycho, leave me alone
*bamm*
*fomp*

Author:  Kingnoz [ Fri May 30, 2003 3:26 pm ]
Post subject: 

here u go, i will give u a post Laughing

Author:  Andy [ Sat May 31, 2003 11:17 am ]
Post subject: 

thanx Wink

Author:  JSBN [ Sat May 31, 2003 11:32 am ]
Post subject: 

nice job, +10 bits

Author:  Andy [ Sat Jun 07, 2003 6:48 pm ]
Post subject: 

hey tony, you should make my arrays tutorial as a sticky instead of yours, mine's much better, rite?

Author:  vexd [ Sun Nov 23, 2003 12:09 pm ]
Post subject: 

hey thanks for the info

Author:  Andy [ Sun Nov 23, 2003 3:42 pm ]
Post subject: 

yes! my post is back up top! lol

Author:  Thuged_Out_G [ Sun Dec 07, 2003 8:53 pm ]
Post subject: 

maybe you should consider adding how to delete elements of an array...im a decent turing programmer...and i cant figure out how to do it Confused ...lol

Author:  Andy [ Sun Dec 07, 2003 9:30 pm ]
Post subject: 

just move every element up after the one u want to delete, and use the flexible arraything... i'm pretty sure you can shorten the length of the arry

Author:  netninja [ Tue Feb 10, 2004 5:20 pm ]
Post subject: 

REWRITE OF WHOLE QUESTION:

I would like to make a var that has more than one value, for eg. var blah, and an array, with numbers 1..10 for eg. and each of those 10 vars has a value of 10,30,40,50 for eg. and also the value of lucy, betty, alicia... etc

How would i go about doing that??

Author:  Andy [ Tue Feb 10, 2004 7:11 pm ]
Post subject: 

then u gotta read up on my records tutorial... found here http://www.compsci.ca/v2/viewtopic.php?t=2325

Author:  Thuged_Out_G [ Fri Feb 27, 2004 11:58 pm ]
Post subject: 

how do you change the upper bounds of a 2d array?

Author:  zylum [ Sat Feb 28, 2004 1:50 am ]
Post subject: 

you can't:

Quote:
In the current implementation (1999), with a multi-dimensional array with a non-zero number of total elements, it is a run-time error to change any but the first dimension (unless one of the new upper bounds is one less than the corresponding lower bound, giving 0 elements in the array) as the algorithm to rearrange the element memory locations has not yet been implemented.

Author:  AsianSensation [ Sat Feb 28, 2004 2:53 pm ]
Post subject: 

Thuged_Out_G wrote:
how do you change the upper bounds of a 2d array?


with multidimensional dynamic arrays you could, you can't change the lower bound though.

multidimensional flexible array works in 4.0.5

Author:  Jekate [ Mon Mar 08, 2004 8:20 pm ]
Post subject: 

I got eh first part of the arrat stuff but the second part is confusing.

for i : 1..200
names(i) = " "
end for

is where i got lost.
pls help.

Author:  Paul [ Mon Mar 08, 2004 8:21 pm ]
Post subject: 

it gives all 200 of the variables in the names array the value of " "

Author:  Jekate [ Tue Mar 09, 2004 8:19 pm ]
Post subject: 

ok so if i got

for i : 1..200
names(i) = " "
end for

Then I got 200 variables and the value of each variable is whatever I put inbetween the " " correct?

so what if I need to change the variables in the program for example

i need each "i" variable to add 1 each time in a loop like this

i(1) = i(1) + 1


will that work?

Author:  AsianSensation [ Tue Mar 09, 2004 8:25 pm ]
Post subject: 

i(1) = i(1) + 1 increases whatever value i(1) used to have by 1. So if that's what you want, then yes. But notice, if you ran that in a loop, only i(1) will be increased. If you want every element in the array to increase, then you have to do this

for rep: 1..100
i(rep) := i(rep) + 1
end for

Author:  Jekate [ Tue Mar 09, 2004 8:32 pm ]
Post subject: 

ok. I got another problem:

for count : 1..58
name(count) = "0"
end for


it says "name has not been declared"
"count has not been declared"

Author:  AsianSensation [ Tue Mar 09, 2004 8:36 pm ]
Post subject: 

Turing editor is really helpful, cuz it tells you what problem you have and you can see it instantly.

"name not declared" simply means you didnot declare a variable of and array of strings called name. Also, to assign stuff in turing, it's ":=" instead of "="

Author:  Jekate [ Tue Mar 09, 2004 8:40 pm ]
Post subject: 

I think that's what I got. At the top of the program, the title of it says:

OOT Editor - Object Oriented Turing (3.1.1A (32-bit))

Author:  SuperGenius [ Wed Mar 10, 2004 10:36 pm ]
Post subject: 

you know dodge, if you're a patient man you could land a job teaching my comp sci class easy. Sure, the pay might not be the best but you get to own the leafs... and the raptors... but no one cares about them.

Author:  Tony [ Wed Mar 10, 2004 10:39 pm ]
Post subject: 

Laughing

you could teach a compsci class just by printing out tutorials from compsci.ca and handing them out Laughing

Author:  recneps [ Thu Mar 11, 2004 4:04 pm ]
Post subject: 

Sadly, some of the tutorials here teach me more than my teacher did, just one of em. Whats with teachers these days? :/

Author:  Cervantes [ Thu Mar 11, 2004 4:27 pm ]
Post subject: 

my teacher taught me relatively little but he's still a great teacher. Compsci is not about learning the syntax of various commands, learning how to use the commands, and learning how to combine various commands and techniques to make a program. Compsci is about doing all that as much as you can by yourself, and about solving your own problems. Compsci teachers should are not like math teachers who stand by the chalkboard and teach concepts and run through various questions. Compsci teachers are more of a guide for students.

Author:  bevoyleick [ Wed Mar 17, 2004 10:12 am ]
Post subject: 

i don't really understand the upper and under part; were those the lines that tells the comp which # are in the upper range/lower range, or is it a predefined command?

Author:  AsianSensation [ Wed Mar 17, 2004 2:21 pm ]
Post subject: 

upper returns the higher bound of array. so
code:
var name : array 4 .. 17 of string
put upper (name)
will output 17 to the screen. This is usually used to keep track of the number or positions of elements in an array.

Similarly, lower returns the lower bound of the array, so in the above example, it will return 4.

Author:  Viper [ Fri Nov 26, 2004 12:29 pm ]
Post subject: 

wow after reading this i jus got more confused


: