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

Username:   Password: 
 RegisterRegister   
 [Tutorial] Arrays
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Andy




PostPosted: 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)
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Fri May 30, 2003 3:08 pm   Post subject: (No 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*
Kingnoz




PostPosted: Fri May 30, 2003 3:26 pm   Post subject: (No subject)

here u go, i will give u a post Laughing
Andy




PostPosted: Sat May 31, 2003 11:17 am   Post subject: (No subject)

thanx Wink
JSBN




PostPosted: Sat May 31, 2003 11:32 am   Post subject: (No subject)

nice job, +10 bits
Andy




PostPosted: Sat Jun 07, 2003 6:48 pm   Post subject: (No subject)

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




PostPosted: Sun Nov 23, 2003 12:09 pm   Post subject: (No subject)

hey thanks for the info
Andy




PostPosted: Sun Nov 23, 2003 3:42 pm   Post subject: (No subject)

yes! my post is back up top! lol
Sponsor
Sponsor
Sponsor
sponsor
Thuged_Out_G




PostPosted: Sun Dec 07, 2003 8:53 pm   Post subject: (No 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
Andy




PostPosted: Sun Dec 07, 2003 9:30 pm   Post subject: (No 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
netninja




PostPosted: Tue Feb 10, 2004 5:20 pm   Post subject: (No 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??
Andy




PostPosted: Tue Feb 10, 2004 7:11 pm   Post subject: (No subject)

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




PostPosted: Fri Feb 27, 2004 11:58 pm   Post subject: (No subject)

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




PostPosted: Sat Feb 28, 2004 1:50 am   Post subject: (No 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.
AsianSensation




PostPosted: Sat Feb 28, 2004 2:53 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: