BBC BASIC
« Referencing »

Welcome Guest. Please Login or Register.
Feb 18th, 2018, 12:18am


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Referencing  (Read 60 times)
mrcamwin
New Member
Image


member is offline

Avatar




PM


Posts: 2
xx Referencing
« Thread started on: Feb 8th, 2018, 1:41pm »

Not sure the subject title is accurate but here goes...

I have an array (9,9,9) so 1000 slots. Randomly throughout this are 'objects'. I dynamically create another array based on the number of 'objects' there are in the main array. I have stats on the 'objects' in the second array. The first three stats I have in the second array are the (x,y,z) coordinates of the object in the main array.

I want to be able to access the stats in the second array based on varying (x,y,z) coordinates from the main array.

So I could be at (x=1, y=2, z=1) so the reference would be 121, but this could be in any place in the second array.

The xyz coordinates are ordered from smallest to largest in the second array.

Ideally I would like something like Excels VLOOKUP to lookup the xyz I have from the main array in the second array.

I suppose I am saying I want to access the second array on the x,y,z values not the location of the item I want in the second array.

Any ideas ?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 727
xx Re: Referencing
« Reply #1 on: Feb 8th, 2018, 9:15pm »

on Feb 8th, 2018, 1:41pm, mrcamwin wrote:
I want to be able to access the stats in the second array based on varying (x,y,z) coordinates from the main array.... The xyz coordinates are ordered from smallest to largest in the second array.

If I have understood correctly, you want an efficient search strategy that will quickly find the required entry in the second array.

This is what I would do in that circumstance. Firstly, rather than store the x, y and z values separately in the second array, I'd combine them as a single integer (for example x + 10*y + 100*z) in such a way that it is ordered from lowest to highest in the array.

Then you can use a 'binary chop' to find the wanted entry; that's very fast, taking a maximum of LOG2n iterations, where n is the number of elements in the array. The binary chop is illustrated in flow-chart form in the Help file here and there's an article with example code at the Wiki here.

Richard.
User IP Logged

mrcamwin
New Member
Image


member is offline

Avatar




PM


Posts: 2
xx Re: Referencing
« Reply #2 on: Feb 9th, 2018, 11:31am »

Cheers Richard - I'll give that a go, thanks.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls