Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Simple Array Question?

Author
Message
DBman
21
Years of Service
User Offline
Joined: 16th Nov 2003
Location: pa
Posted: 12th Feb 2004 01:43
I was just wondering if anyone could explain to me exactly what an aray is and how to use one the DarkBasic pro refrence states under DIM page 7 "u are able to creat arrays with up to 5 demensions. Arrays are best visualised as a line of boxes in these boxex are 11 smaller boxes contained in thes baxes are single itoms of data" I can't see how to visualise this or even find a decent example on hoe to use an array if anyone can help it would be verry aprisheated THANX
DBMAN
P.S. this is on the basic array nothing advanced and im a tarable speeler sorry
hexGEAR
22
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 12th Feb 2004 03:12 Edited at: 12th Feb 2004 03:15
Example:

say in a role playing game (like zelda) i have a game character, and for this character i have to store his:

- life, 10
- majic, 50
- power, 100
- speed, 400
- accuracy, 30

now, i could make 5 seperate integer variables to store all these values, like life = 10, majic = 50, power = 100, speed = 400 and accuracy = 30 which is perfectly ok but say you want to like check if each variable is above a certain value 200, you would have to do something like:

if life > 200 then
if majic > 200 then
if power > 200 then
if speed > 200 then
if accuracy > 200 then

an alternative is to use arrays, you could create a single array to store all this information rather than making 5 seperate variables. sine there are 5 stats we want to store, we need to create an array with 5 spaces. Arrays are created using the DIM statement, and you can call your array anything you want, i'll call this array "stats", so:


dim stats(4)


you may be wandering why that i have "4" and not "5" in there, well the space stats(0) will be used for life, stats(1) for majic, stats(2) for power, stats(3) for speed and stats(4) for accuracy. so you would have:


stats(0) = 10
stats(1) = 50
stats(2) = 100
stats(3) = 400
stats(4) = 30


now, if you go back to the searching to see if all stats are above 200, you can change the code and write this instead:


for x = 0 to 4
if stats(x) > 200 then
next x


see how easier and neater it is, you may not notice it now but you will as you advance.

--------------------------------------------------------------------

Thats was a 1 dimension array, to visualise a 2 dimension array, say we wanted to store the same stats, life, majic, power, speed and accuracy not for just our main character, but for 10 different characters, you can use the same array to do this. Think about it, 10 characters, each having 5 stats:


dim stats(9,4)


i hope you now understand why i have "9" there and not "10", because 0 is also considered. So the stats of all will be as follows:

character 1

stats(0,0) = 10
stats(0,1) = 50
stats(0,2) = 100
stats(0,3) = 400
stats(0,4) = 30


character 2

stats(1,0) = 10
stats(1,1) = 50
stats(1,2) = 100
stats(1,3) = 400
stats(1,4) = 30


etc till you reach character 10 with stats(9,0) to stats(9,4). Go back to the search to see if all characters have stats above 200, imagine the number of variables we would have had to declare to store this information, and the number of "if" statements we would need to write to check each and every variable, with arrays all this can be done in 5 lines:


for x = 0 to 9
for y = 0 to 4
if stats(x,y) > 200 then
next y
next x


that loops round the entire stats array and checks each individual value is greater than 200. Hope you understood.

phew, i got lots of time on my hands

walaber
21
Years of Service
User Offline
Joined: 22nd Oct 2003
Location: Los Angeles, CA
Posted: 12th Feb 2004 10:20
hexgear's explanation is very nice, I think that should explain it.

you can also think of an array as a file cabinet. for example the following array:

dim FileCabinet(5,10) as string

this array has 5 drawers, and each drawer can hold 10 items. to put the name "bob" in the first slot of drawer number four, do this:

FileCabinet(4,1) = "Bob"

etc...

you may notice that unlike Hexgear I don't use the 0 slot. this is just because I find it easier to start from the number 1, not 0. in reality the array above has 6 drawers, and 11 items per drawer. but whatever way you want to think about it!

good luck

Go Go Gadget DBPRO!

Athlon XP 2400+ || DDR-SDRAM 1GB || Nvidia Ti4200 AGP 8x 128MB
DBman
21
Years of Service
User Offline
Joined: 16th Nov 2003
Location: pa
Posted: 12th Feb 2004 17:05
Thanx guys those were great explonations. one last question dose anyone know a source code or snipit that uses thes examples so I can see how they are used in a real program and code?

THANKS,
DBMAN

Login to post a reply

Server time is: 2024-11-26 03:55:24
Your offset time is: 2024-11-26 03:55:24