Arrays
Blitz3D Forums/Blitz3D Beginners Area/Arrays
| ||
can some PLEASE explain the concept of arrays and multi-dimensional arrays?? i just am really having trouble understanding what they're used for, how you access them, and things like that. also, here is code for a calculator program. i realize its a very bad way to do a calculator, but its the example that was used in the book. anyway, how is an array being used in this prog, and why do you need it?? im new here, so thank you very much for all your help;op1 and op2 are global so they can be accessed from all functions ;op1 contains first operand, op2 contains second Global op1 Global op2 Dim array(100) ;1-100 initializearray() ;continue is 1 as long as program is running continue=1 While continue ;as long as the computer wants to play ;get the first operand op1 = Input("What is the first number? ") ;get the second operand op2 = Input("And the second? ") ;what does the user want to do? operator$ = Input("Enter +, -, /, or * ") ;print the answer printanswer(operator$) ;find out if user wants to continue continue = Input("Enter 1 to continue or 0 to quit ") ;insert a new line Print "" Wend End ;this function sets up the array Function initializearray() For i=0 To 100 array(i) = i Next End Function ;this function prints the answer to the expression Function printanswer(operator$) Print op1+" "+operator$+" "+op2+" is equal to "+findanswer(operator$) End Function ;this function performs the math based on the user input Function findanswer(operator$) Select operator Case "+" Return array(op1) + array(op2) Case "-" Return array(op1) - array(op2) Case "*" Return array(op1) * array(op2) Case "/" Return array(op1) / array(op2) End Select End Function |
| ||
Arrays are special 'groups' of variables. A standard one-dimensional array can basically be thought of as a line of variables that can each hold a value. A two-dimensional array can be thought of as 'rows' of lines of variables (or rows of one-dimensional arrays). Each array element can hold a value. What this value means is dependent on how you want to use it. Arrays help the programmer save time by allowing him to not have to type a lot of redundant code. Let's say for example that you were writing a game that had a hundred characters in it. Each character has an X and Y position. You could create 200 variables the long way: Player1X = ? Player1Y = ? Player2X = ? Player2Y = ? Player3X = ? Player3Y = ? etc... Or you could create a 100 element array that holds both of those values (two-dimensional) for you: Dim PlayerPos(100, 2) The first value in the array [PlayerPos(thisvalue,?)] is the number of characters in your game (you could think of them as rows). The second value in the array [PlayerPos(?,thisvalue)] contains the X and Y positions for each character (you could think of them as columns). Let's say you wanted to move character 46 to position 32,143. You would do so by doing this: PlayerPos(46,1)=32 ;the X value PlayerPos(46,2)=143 ;the Y value As for what they are using the array for in the above code sample, I have NO idea. The code sample definitely doesn't need the array to function. It will work without the array if you remove all references to the array from the code. That code sample that you provided is a VERY poor array example. Forget about it and study a different one. |
| ||
I think there is another very important property of arrays, usually overlooked because it is so obvious. Simply that an array is a device through which a collection of variables is mapped onto a collection of numbers (indices). This means that the indices can themselves be thought of as a collection of variables - and so on - to any required depth of recursion. It's essentially much the same trick as functions being able to call other functions. The ability of a language to talk about itself, to interchange data and instruction,i.e. metalanguage, is a powerful and useful property. |
| ||
Big Shooter, I don't know what book you got that code from but I'd be tempted to throw it away as it's a very poor demonstration of array usage. In fact, the array in that example is not needed at at! And naming the array 'array' isn't exactly helpful, either. Anyway, an array is just a group of variables of the same type (%,#,$ etc.) that are related in some way. For example, you could have an array that holds the names of your 5 favourite films: Dim fav_film$(10) fav_film$(1)="Alien" fav_film$(2)="Rocky" fav_film$(3)="Raiders of the lost ark" fav_film$(4)="The Wizard of Oz" fav_film$(5)="The Texas chainsaw massacre" You can then use an 'index' number to access any of the array vaiables (elements) you need, such as: num = Input("Enter a number between 1 and 5") Print "My number " + num + " favourite film is " + fav_film$(num) Notice that this wouldn't be possible if we'd stored all the film names in separate variables! |
| ||
wow you guys, that information is SO helpful. you guys are awesome!! thank you very much!! |
| ||
multi-dimensional arrays make it a bit more interesting. Think of your array as a chess board. dim chess$(7,7)will give you 0-7 across and 0-7 down (64 square in total). So you can reference a point and see what's there or set what's there. chess$(5,5) = "Black Knight" chess$(1,2) = "White Queen" print chess$(3,3)Then you can take it even further and stack several chess boards on top of one another. dim chess$(7,7,2)This has stacked 3 chess boards on top on each other. You can now reference any squre on any board. This sets position 5,2 on chess board 1 chess$(5,2,1) = "White Bishop" print chess$(5,2,1)Sets position 2,1 on chess board 0... chess$(2,1,0) = "Black Pawn"You ready to take it even further... Well, lets have our stacked chess boards spanning across different rooms... dim chess$(7,7,2,9)You getting the idea? We now have 10 (0-9) rooms we are playing our 3 stacked chess games in. We can reference any square on any chess board in any room... chess$(5,1,2,7) = "Black Rook"So, in room 7, on board 2, at position 5,1 on that board there is now a black rook... Expand it further... Lets add any building... dim chess$(7,7,2,9,4)we now have x,y on the board, board number, room number, and building number which we can instantly reference... chess$(1,2,2,8,3) = "White Pawn"So the piece at 1,2 on board 2 in room 8 in building 3 is a white pawn. I hope I haven't confused you! |
| ||
Note that Blitz arrays start at zero so a line like this:Dim Array(0)is a perfectly acceptable line (and would contain 1 array element). A line like this: Dim Array(0, 0, 0)is also a perfectly acceptable line (and would actually still contain only 1 array element (1 times 1 times 1 equals 1)). A line like this: Dim Array(1)would contain 2 elements (0 and 1). And a line like this: Dim Array(1, 1, 1)would contain 8 elements (2 times 2 times 2). |
| ||
awesome, thank you guys. all this info is very helpful |
| ||
I have a question about arrays too. Is it possible to pass an array to a function? If so how? Say I want a function to copy one array to another for example. |
| ||
You can copy an array to another with a simple For/Next loop: that basically says For a=1 to ????: Array2(a)=Array1(a): Next - will work with string arrays too. You can do it in a function or whatever you want. I wouldn't have thought you can just pass the entire thing without doing it in some kind of loop. |
| ||
I thought it wasn't possible, but: http://www.blitzbasic.com/Community/posts.php?topic=35181 As far as I know (I better take my precaution) you can only do so if the array is stored in a type. |
| ||
Arrays are global so you don't need to pass them to functions. |
| ||
Note that the (undocumented) arrays[] that ebusiness speaks of are not the same as the "true" arrays() that Rob speaks of. The former are sometimes called BlitzArrays or Blitz Arrays which can be defined locally or globally, and as part of types, but only in one dimension. The latter must be defined globally with the Dim statement, can be multiple dimensions, but cannot be elements of a custom type. |