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.

AppGameKit Classic Chat / Whats the point of multidimensional arrays?

Author
Message
Stormwire
14
Years of Service
User Offline
Joined: 3rd Sep 2010
Location:
Posted: 3rd Jul 2013 22:50
Is there anything that multidimensional arrays can do that 1d arrays cant?
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 3rd Jul 2013 23:01
No, a single array can do anything a multidimensional array can do.

Although multidimensional arrays sometimes provide a better way of logically separating your data. For example, an 8x8 array could represent a chess board, you could have a single dimensional array of 64 but there would be extra work to retrieve any individual square(eg. 3 across and 4 up).

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Jul 2013 23:01
Probably not.

But using multi-dimensional arrays can be very useful.

A simple case would be using an array to some value for every box in a gridded display.

If you use two dimensions, you can access the point you want quickly (code wise): x = myarray[r][c]

If you use a one-dimensional array, you need to calculate the position (assuming zero based): x = myarray[r*mywidth+c]

Yes, that is exactly what happens behind the scenes in the compiled version. But it is not as clean looking from a code point of view.

Similarly, a three dimension array would let you directly access any 3D block of space in a defined space.

There are lots of other very good uses for multi-dimensioned arrays.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Stormwire
14
Years of Service
User Offline
Joined: 3rd Sep 2010
Location:
Posted: 4th Jul 2013 00:42
Thanks @Matty H @Ancient Lady

Quote: "A simple case would be using an array to some value for every box in a gridded display."


Exactly what I was trying to do before I got confused. Here's the thing, if grid[x, y] equals one value and grid[int] is equal to one value also then how is that of any use? You always get just one value out. That might be a really stupid question lol

What I was originally thinking before everything became messy was to store the X and Y values of dots in an multiD array and then extract the values in a loop as I create the dots. I got everything working except the extraction part.

The thing that's doing my melon in is I can only store and extract X or Y not X and Y.

Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 4th Jul 2013 00:56 Edited at: 4th Jul 2013 01:00
I think you may be over-thinking it.

Generally when you store griddy stuff in a multiD array, you store the contents not the coordinates The coordinates get you to the contents as indexes in the array.

grid[x,y] = value

However if you have a lists of things and you want to store their coodinates in an array, the list is one thing so you would use a 1D array. To store several things in a 1D array you need a custom type;

then you can use stuff like;

MyObject[ count ].xpos = thisX
MyObject[ count ].ypos = thisY
Stormwire
14
Years of Service
User Offline
Joined: 3rd Sep 2010
Location:
Posted: 4th Jul 2013 01:04
Thanks Marl, Im gonna go the Type route, it seems simpler.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 4th Jul 2013 01:21 Edited at: 4th Jul 2013 01:36
There is at least one problem with this code:


You are assigning a value "grid[t,i] = x" outside of the loop that you define 'i' in. The first iteration through the 'For t' loop will have 'i' at zero (first use of undefined variable in AppGameKit is always zero). After that it will be set to count + 1, the value it was at when the 'For i' loop finishes. And the first line in your 'For t' loop is wiping out that x value.

And I really don't understand what you are trying to do. If you are trying to store the y value in the grid, there is no need to slide the second index, your 'i', the way you are doing.

I do know that the DrawLine function does not draw a point if the start and stop positions are the same. This was discovered by someone else in some other thread.

Try this (it creates a grid of dots and the file has the correct values):


Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Stormwire
14
Years of Service
User Offline
Joined: 3rd Sep 2010
Location:
Posted: 4th Jul 2013 01:37 Edited at: 4th Jul 2013 01:48
They must have fixed the DrawLine command. Im using the latest beta. Heres what I got when I fixed the x position.



Quote: "
You are assigning a value "grid[t,i] = x" outside of the loop that you define 'i' in.
"


Thanks for that. I think thats why I am getting the gap in the middle.

Edit

just seen your code. Thats very helpful thanks!

Login to post a reply

Server time is: 2024-11-24 15:59:19
Your offset time is: 2024-11-24 15:59:19