Computer Science Canada [tutorial] Arrays |
Author: | Tony [ Mon Mar 10, 2003 1:09 am ] | ||||||||||||
Post subject: | [tutorial] Arrays | ||||||||||||
Many people are unfamiliar with the use of arrays and they dont know what they're missing! Arrays are EXTREAMLY useful in almost any of your programs. What is an array? An array is essensially a group of variables of same type, put together into one group for organization and easier access. Think of it as a row of boxes, each containing a value. Why not just declear a bunch of variables? Have fun... Thats like buying a single ticket to Wounderland (in toronto). If you know you'll have atleast 2 variables, its cheaper to declear an array. Besides, it doesnt get preaty when you have a user defined amount of variables needed. How to declear an array variable
name - the name of your variable :array - type... says its an array 1..10 - range of the array. Here we have 10 "boxes". You can have more if you want of string - means our "boxes" store string values. You can replace it with int, real, boolean, or w/e else you want (types for example... more on them in separate tutorial). How to use an array You use an array just like you would a normal variable, but you specify what "box" from your array you want to open inside the brackets after the name of the variable. name(1) The main advantage of the array is that you can get the program to select which "box" to get the value from. Since you can put a variable inside the brakets, you can get the program to pick the index (which box).
this way instead of saying put name1 put name2 ... put name100 you can have a simple forloop do it for you. You can also put a couple of if statments in there to check for stuff like spelling of the name and output only if it starts with sertain letters for example. Some array tricks - 2D arrays Here's something that I used to scare people with right before grade10 compsci exam... "you need to know 2D arrays". People were freaking out... I donno why Anyway, the point is that each of our little "boxes" in an array can contain a row of boxes of its own. So for each box in a row, we get another row, forming columns. This way we just created a grid (also called matrix). it's decleared as
5 rows, 10 columns. Though that depends how you look at it and how you set up your forloops. I had a huge argument with my teacher of what value gous first - row or column. I dont remember what she said, but in my opinion it doesnt matter - as long as you know How to program 2D array Declaration (again)
Initialization of values (we need our array to hold something).
We loop over every row and every colum and build a random map. And now to display the contents:
that .. on 3rd line keeps output on same line of text put "" forces output onto next line after a row is done More array tricks - 3D array same idea as before... a row of boxes inside a box thats inside a box. Now each box in a column has a row of its own going along Z axis, creating a 3D matrix to store our values. To program it, just and another forloop for the 3rd value. Thats about where our geometric imagination ends. But programming doesnt stop here. You can create 4th and 5th and if you try hard, even 6th demention in your array. Your need for dimentions will run out faster then you'll find the limit. I cant really think of an example for a 3D array even... Maybe some record keeping setup where row is some group ID, column is name and z-axis is information about the name. User defined size of array You can declear array size with a variable. var name:array 1..number of string I dont know if that makes any difference though. As you know (and if you dont, then you will know this now) that turing converts your code to C++ before compiling it. And in C++ you cant have user defined ranges for arrays. So you just put the largest value (under 60000 +-) you know that might be used and hope your program doesnt crash But in case your teachers care, put a variable and have a dynamic range. Just make sure to use the same variable in your for loops so that you dont go over the limit. Conclusion This turned out to be a LONG tutorial, but its well worth it. You'll end up using arrays in most of your programs from now on |
Author: | Vicous [ Wed Mar 12, 2003 8:06 pm ] |
Post subject: | 3d arrays? |
my imagination ends at 2d, Ive never been to wounderland but wonderland was fun (not that that was your only spelling mistake) and, most importantly... How dare you make this tutorial after I pretty much tought myself to use arrays!!! roflol [/i] |
Author: | Tony [ Wed Mar 12, 2003 11:12 pm ] |
Post subject: | |
1st - noone asked me to make any tutorials and even if they did, I dont get paid to do this stuff. I'm still hanging out here cuz I'm such a nice person 8) 2nd - that tutorial is 787 words long (without conting smileys), ofcourse there will be spelling mistakes! If you continue to bug me about my (and dan's ) spelling, I'd be forced to give you LAMER rank... or not give you an email... or use MS word to type up tutorials... whatever's more appropriate |
Author: | Vicous [ Thu Mar 13, 2003 11:39 am ] |
Post subject: | Nooooooo! |
I'm just joking but, you have to admit, if you were in my shoe's, youd find this tutorial annoying too. besides, It's fun making fun of spelling in a forum.... |
Author: | Vicous [ Thu Mar 13, 2003 11:42 am ] |
Post subject: | not ms word! |
oh, and take away the E-mail if yer gonna get mad, ms word would punish everyone |
Author: | Tony [ Thu Mar 13, 2003 12:49 pm ] |
Post subject: | |
you mean with autofixing the code part? |
Author: | Vicous [ Mon Mar 17, 2003 2:10 pm ] |
Post subject: | |
I don't have MS word on my computer. |
Author: | Tubs [ Wed Mar 19, 2003 12:35 pm ] |
Post subject: | |
how can i make an array that i can use in a for statement to put the numbers - 50 .. 50 across midy by 10? |
Author: | Tony [ Wed Mar 19, 2003 1:00 pm ] | ||
Post subject: | |||
var num:array -50..50 of int ofcourse if you go by 10, then you get a BUNCH of unused variables there... :S better have -5..5 and just treat each result as you go by 10s.
|
Author: | Vicous [ Thu Mar 20, 2003 10:54 am ] |
Post subject: | |
and if you need the var to be a specific number for some reason, just multiply it. eg. var num(-5..5) for i:-5..5 put "enter the ",i*10, " variable " get num(i) put num(i)*10 end for |
Author: | Omnipotence [ Sat Oct 01, 2005 11:23 am ] |
Post subject: | |
I was wondering how I could use arrays to change my code: Quote: if ageyear >=1 then if ageyear <2 then put "1 year and ".. put ((ageyear-1)*365)-exdaymon .. put " days old." end if end if To be able to be written only once instead of having to manually increase the numbers. |
Author: | Cervantes [ Sat Oct 01, 2005 11:36 am ] | ||
Post subject: | |||
Why would you want to use arrays for this?
Also, please post Turing questions in the Turing Help forum. |
Author: | Omnipotence [ Sat Oct 01, 2005 3:40 pm ] |
Post subject: | |
Sorry for posting a qustion here >< but I wanted to do that because I have to make an age calculator of the sorts. And the idea I had involved having to calculate the year and day seperate and making some sort of note... its all very convulted... and thank you very much for helping me finish my code! |
Author: | Jenkinz [ Sun May 07, 2006 8:15 pm ] | ||
Post subject: | |||
in this array, why do we need the extra := false at the end of the boolean variable, and what does if compclass (i)= answer then found = true mean, trying to get the just of arrays...
|
Author: | Tony [ Sun May 07, 2006 8:25 pm ] | ||||
Post subject: | |||||
your question is more about flags than arrays. A flag is a variable (usually boolean or an integer) that is used to keep track of a certain state. In your example, found is a flag that keep track - we ether found what we were looking for, or not yet. The initialization var found:boolean := false is used, since when we begin the program, we have not yet found what we are about to start looking for. Later
if a value in our array (compclass) at location (i) is equal to our answer, so there for we found a match and we're saving this information in a flag Later in the program we can return and check if we found the answer previously or not
|
Author: | Velocity [ Tue Nov 08, 2011 7:17 pm ] |
Post subject: | RE:[tutorial] Arrays |
+3 bit |