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 / Need help with arrays (again!)

Author
Message
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 6th Aug 2004 11:30
Somebody wrote somewhere that arrays are supposed to be easy - and as I wa struggling to get my head around them I though a bit of practice was in order. This program is meant to move a ball randomly across the screen, write all of its previous x and y positions into a 2 dimensional array, then print a series of dots (ie. a trail) at all of the positions, then repeat the process every loop as the ball moves.

Problem is, although I've got the x-component of the trail working, the y component seems to be stuck at zero - giving a trail at the top of the screen. Hmmm.......

Anybody with time to kill, could you check my code please?

Thanks!

Richard

CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 6th Aug 2004 12:12 Edited at: 6th Aug 2004 12:13
dim xys#(n,2)

n is zero here so your array is (0,2)
the n number will never go above 0

define the n/0 number to the highest value n will ever be


DBP_NETLIB_v1.4.3 - 65 FREE Functions * Click Logo
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 6th Aug 2004 19:03
Also, it is not usual to have the dim command inside the main loop.

Generally speaking, a two dimensional array is like a series of post office boxes in a hotel;

.. 0 1 2 3 4 5
0 x x x x x x
1 x x x x x x
2 x x x x x x
3 x x x x x x
4 x x x x x x
5 x x x x x x

for and array of DIM mine(5, 5)...
each X in this case is a value that can be any number. So when you do a mine(3,2) = 4, the array looks like;

.. 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 0 0 0 4 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0

and so on. All values start as zero as above, then you stuff in the different "postal letters" or the numbers you want in each box...

Hope this helps.
S.

Any truly great code should be indisguishable from magic.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 7th Aug 2004 05:21
Aha .....thanks - I'm getting there but slowly.


I've sort of got it working now, but I still don't quite get it - you see I had the DIM command in the loop, because n increases indefinitely inside the loop due to the n=n+1 at the end (did you spot that CR?), but I wanted the array to grow in size as n increased indefinitely (or until the computer runs out of memory!) so that it could store all the past positions of the ball and draw the trail of dots.

Now, if when the array is recreated, on each loop, the contents of it were cleared in the process, then I could understand why all the values except the most recently entered ones would come out as zero. But the thing is, is that for some reason it was storing the x-positions in (n,1) ie. the top row - just not the y-positions in (n,2) ie. the bottom row. (You can see the trail being drawn at the top of the screen where Y=0.)

I did get it working by simply taking the dim out of the loop as you suggested, then predefining n as a very large number. So it works, but I still don't get why it was storing row 1 but not row 2.

I'm the sort of person who, even once it's working, needs to know why it wasn't working in the first place!!
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 7th Aug 2004 05:53 Edited at: 7th Aug 2004 05:53
it could just be compiler bug, basic arrays are set to a size and then never altered, DB Pro allows you to delete and remake arrays (a cool feature), it also allows you to add and remove elements from them, so that you can use them as a growing list or stack of information (a realy cool feature), the reason you still got your x values is probably due to there being a few small bugs in the arrays yet (I seem to recall a post in the bugs forum about some array issuse after P5), when you remade the array at the new size (if the system allowed it) it should have initialised at zero for all the elements (the locations in the array), in fact actualy it should have stopped with an "array already exists in line xxx", I guess it still needs some work in that respect.

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, Nvidia FX5900 gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 7th Aug 2004 06:05
Right - at least it's good to know that it wasn't just my lousy coding!! Thanks.

Richard

Login to post a reply

Server time is: 2024-11-27 04:37:22
Your offset time is: 2024-11-27 04:37:22