Qbasicnews.com

Full Version: Arrays
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Does anyone have a good EASY to understand definition of arrays and how to use them, and what there used for. I've been told that you use them a LOT in game making, but I never used them even in text based stuff. This is the area where I'm haveing problems.

Its not the actual array that gives me problems, its finding the where you use them and understanding multi-dimensional arrays.
Thanks
~bobrobyn
Think of an array like a collection of values of the same type.

Multi-dimmensional arrays: Think about collections of collections.

For example, you can imagine a mono-dimmensional array like a shelf where you put together numbers.

Code:
CONTENTS: 4 3 6 8 4 2 7 5 4 5
INDEX:    0 1 2 3 4 5 6 7 8 9

If you array is called a%, then a%(7) will equal "5".

You can imagine a two-dimmensional array like a set of shelves where you put together numbers:

Code:
HORZ
       INDEX  0   1   2   3
----------------------------
VERT.    0  | 5   6   8   2
INDEX       |
            |
         1  | 3   3   0   7
            |
            |
         2  | 2   1   9   4

If you array is called b%, then b%(1, 2) will equal "0".
Or, to put in in an alternative way, a multidimensional array just holds a certain amount of numbers for a certain amount of other numbers. Its like having an array inside an array. So, if your array was blah(20,10)

then, for blah(0) you would have 10 subscripts, for blah(1) you would have 10 subscripts, etc, etc, all the way up to blah(20). It is very useful for example if you wanted to hold a small picture.

You would have the array picture(10,10)
this would allow you to have a picture up to 10*10 pixels wide (actually it would allow 11*11 but thats not important just now.)

so then you could take two FOR-NEXT loops like this:
[syntax="qbasic"]FOR x = 1 to 10
FOR Y = 1 to 10
picture(x,y) = the colour at the point (x,y) on the picture
NEXT
NEXT[/syntax]

As you can see, this would easily allow you to hold the data for the picture, in coordinate terms.
Say that your into racing and you want to keep track of some statistics. You want to keep track of wins, top 5's, top 10's, top 20 ......finsihes for 43 drivers. So your array would be
driver(43,4).......43 drivers and 4 catagories.


so driver(1,1) would be the first driver and number of wins
driver(1,2) would be the first driver and number of top 5's
driver(1,3) 1st driver top 10's
driver(1,4) 1st driver top 20's
ect.....

and driver (43,1) would be the 43rd driver and number of wins
and so on.
I suppose we'll ave to define index.

-A part of an array that holds data (can be numbers or strings), and is listed as a number

DIM array(1 to 10) has 10 indexes, and each can hold 1 piece of data (unless we use the TYPE command.......don't worry about that yet)


You can also have multiple indexes

DIM abc(1 to 10, 1 to 2)

abc(1, 1) is different from abc(2, 1), because one index has changed.....this is usually used in Mapping...Map(x, y) and the data of an index is a tile or colour index.....

I suppose this is really confusing you, but thats what we're here for....confuse then learn... lol

Alex~
While we are on the topic of arrays, I noticed that you cannot include a DIM statement... thus not able to put arrays within user defined types...

Is it possible, in any way, to include an array within a user defined type.

For example, in my data structures and algorithms class, we learned about queus, and how you have a queue type which contains the queue as well as a count.. so, you could say

queue.data(queue.count) = data

something like that... is this possible in qbasic?
bobrobyn: I prefer looking at two-dimensional arrays as checkerboards where each square holds a value (is an element in the array). To access each, the horizontal lines of checks have an "index" and the vertical as well. So if you have:
Code:
1   2   3   4
1 [1] [4] [6] [3]
2 [8] [0] [3] [5]
3 [9] [3] [2] [3]
4 [3] [6] [2] [1]
And you declare the array like this:
Code:
DIM MyArray(1 TO 4,1 TO 4) AS INTEGER
Then MyArray(1,3) is 9, MyArray(2,2) is 0, etc.
Just adding my explanation Wink . Na_th_an's is good too, as is everyone else's.

[Edit]BrettH: I don't believe it is possible, in QB, to put arrays in user-defined TYPEs (well, you could possibly emulate pointers and have it point to an external array, but that would ruin the purpose of a TYPE). However it is still possible to create stack/queue structures with QB, of course. :wink:
Arrays inside types is possible with QB7 only
In fact with BC6, BC7.x and VBDOS Wink
Bleh. Who uses BC6? Wink And you *know* I meant >=QB7 Wink
Pages: 1 2