Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Three-dimensional array
#11
(01-04-2023, 10:12 PM)Kernelpanic Wrote: Well, I can't think of a practical application right now. Maybe something for mathematicians who can store three-dimensional functions in it. It would be impractical for a "normal" database. I would take two or three records that access each other. For example:

Code: (Select All)
Type employee
   firstName As String * 20
   lastName As String * 20
   birthday As As String * 10
EndType

Type function
   programmer As employee
   salary as employee
   . . .
EndType

The 3D array is a nice exercise, and I haven't found anywhere that shows a graphical representation of a 3D array.

https://www.qb64tutorial.com/lesson8

Everything you ever wanted to know about 1D, 2D, 3D, and 4D arrays, with examples. Big Grin
Reply
#12
one tip:
_PRESERVE works only with monodimensional array... 
moreover it is the exact duplicate of _PRESERVE of QB7.1 / QB PDS

run this code to catch the issue
Code: (Select All)
'$dynamic
ReDim threeDim(1 To 10, 1 To 5, 1 To 3)
Screen _NewImage(100, 30, 0)
For a = 1 To 10
    For b = 1 To 5
        For c = 1 To 3
            threeDim(a, b, c) = a + (b * 10) + (c * 100)
            First$ = First$ + Str$(threeDim(a, b, c)) + "/"
    Next c, b
    First$ = First$ + "|"
Next a
Print First$
Print "<------------------------------------------------->"

ReDim _Preserve threeDim(1 To 12, 1 To 5, 1 To 5)

For a = 1 To 12
    For b = 1 To 5
        For c = 1 To 5
            Second$ = Second$ + Str$(threeDim(a, b, c)) + "/"
    Next c, b
    Second$ = Second$ + "|"
Next a
Print Second$

This lets suppose that array is a stack and not a set of cell of memory ordered by pointers.
Reply
#13
"_PRESERVE works only with monodimensional array... "

Yeah, I believe there was a whole discussion on this at a previous forum. If I remember correctly _PRESERVE only works on one of the dimensions of a multidimensional array which makes for very confusing situations. This is why I have always stuck with using single dimension arrays paired with a TYPE definition to get 2D structure but still able to use _PRESERVE.

Array usage in QB64 is more than adequate for most tasks but I would welcome more features. Many other languages, such as Python, are extremely flexible with their array usage (which are called lists and tuples in Python).
Reply
#14
(01-05-2023, 09:07 PM)TempodiBasic Wrote: one tip:
_PRESERVE works only with monodimensional array... 
moreover it is the exact duplicate of _PRESERVE of QB7.1 / QB PDS

This is for sanity reasons. Otherwise a programmer "for hobby" is going to want to change the extent of one of the dimensions as well which is a nightmare to maintain on the developer's side.

On BASIC PDS v7.1 the helper keyword did not carry an underscore, it was imported as if it were an original QB64 keyword.

Of course there is a way to get around the limitation, but have to get away from the convenience of declaring and using arrays. Must create a _MEM buffer, extend it as necessary and do the dimensional calculations to access bits of data. Not pretty but doable.

Arrow Terry answered before I could finish LOL.
Reply
#15
(01-04-2023, 11:41 PM)TerryRitchie Wrote: https://www.qb64tutorial.com/lesson8
Everything you ever wanted to know about 1D, 2D, 3D, and 4D arrays, with examples. Big Grin

Your example does not show a real three-dimensional array, but three two-dimensional ones. I don't know now how I for example, a three-dimensional array "Sales" should represent 12 months, 30 days, 52 weeks.

This is a real three-dimensional field:
Code: (Select All)
$Console:Only
Option Base 1

Dim As Integer dreiDimFeld(3, 5, 7)

Dim As Integer dm, dz, ds, dFeld
Dim As Integer ebene, zeile, spalte

Locate 2, 2

dFeld = 1
For dm = 1 To 3
  For dz = 1 To 5
    Locate CsrLin + 1, 2
    For ds = 1 To 7
      dreiDimFeld(dm, dz, ds) = dFeld
      Print Using "### "; dreiDimFeld(dm, dz, ds),
      dFeld = dFeld + 1
    Next
  Next
  Print: Locate , 2
Next

Locate CsrLin + 2, 2
Input "Zeige Wert in Ebene : ", ebene
Locate CsrLin + 0, 2
Input "Zeige Wert in Zeile : ", zeile
Locate CsrLin + 0, 2
Input "Und in Spalte       : ", spalte

Locate CsrLin + 1, 2
Print Using "Wert in Ebene: # Zeile: # Spalte: # ist: ###"; ebene, zeile, spalte, dreiDimFeld(ebene, zeile, spalte)

End

When displaying months, days, weeks, etc., one could do something like this; just an idea, not tested.
Code: (Select All)
$Console:Only
Option _Explicit

Dim As Integer ebene, zeile, spalte

Option Base 1
Type InternetHandel
  monate As String * 15
  tage As Integer
End Type

Dim Umsatz(1 To 30) As InternetHandel

PS: Something just went wrong: No access to the server.
Reply
#16
Pretty sure PRESERVE works with multiple dimension arrays. I think my SQL stuff that depends on a two-dimensional array wouldn't work if PRESERVE only handled one dimension arrays.
Tread on those who tread on you

Reply
#17
(01-06-2023, 03:48 PM)Spriggsy Wrote: Pretty sure PRESERVE works with multiple dimension arrays. I think my SQL stuff that depends on a two-dimensional array wouldn't work if PRESERVE only handled one dimension arrays.

It works, but only on one dimension.

REDIM x(10,10)

REDIM x(10,20) <-- this is fine.

REDIM x(20,10) <-- this will corrupt your index.

The reason is real simple -- redim just copies the existing mem block and moves it to a new memblock, and data is stored in memory as a 1d block of memory.

For example:

12
34
56
78

The above is a 2 x 4 array of single digits.   In memory, it's stored simply as "12345678". Left to right, top to bottom.

Now, redim and add a column, you get:

123
456
780
000

Corrupted data!  Sure, you added 4 bytes to the whole array, but when you moved that "12345678" over with those 4 new "0000" that you just created, they map to the index in the manner above.

So, why can you create a new row with no problems??

12
34
56
78
00

See where your "12345678" copied over and remained in the same index positions?  Those two new "0" characters are at the end of the data, but the structure of the data itself hasn't changed.   We still read it left-to-right, top-to-bottom.

Multi-dimensional arrays are only redim _preserve-able with the last element.  Anything else, you need to move manually into a new array yourself.
Reply
#18
Code: (Select All)
'visual representation of a 3-dimensional array
Screen _NewImage(800, 600, 32)
_PrintMode _KeepBackground
Dim Shared cube$(9, 9, 9)
For z = 1 To 9
    For y = 1 To 9
        For x = 1 To 9
            cube$(x, y, z) = _Trim$(Str$(x)) + _Trim$(Str$(y)) + _Trim$(Str$(z))
        Next x
    Next y
Next z
Do
    For n = 1 To 12 Step 0.25
        _Limit 6
        Cls
        For z = 1 To 9
            Color _RGB32(z * 25 + 25, z * 25 + 25, z * 25 + 25)
            For y = 1 To 9
                For x = 1 To 9
                    _PrintString (x * (n * 4) + z * (n * 1.5), y * (n * 4) + z * n), cube$(x, y, z)
                Next x
            Next y
        Next z
        _Display
    Next n
    For n = 12 To 1 Step -0.25
        _Limit 6
        Cls
        For z = 1 To 9
            Color _RGB32(z * 25 + 25, z * 25 + 25, z * 25 + 25)
            For y = 1 To 9
                For x = 1 To 9
                    _PrintString (x * (n * 4) + z * (n * 1.5), y * (n * 4) + z * n), cube$(x, y, z)
                Next x
            Next y
        Next z
        _Display
    Next n

Loop Until InKey$ = Chr$(27)
Reply
#19
I was going to share a program that indicated the shock with Lotus123 starting to support "three-dimensional" spreadsheets, I think in v3. After long enough from "A1" to "IV8192" with a single sheet which was the whole document.
Reply
#20
As Steve suggests the handmade movement of value from old to new array is the solution. 
In the time of discussion about this feature I wrote a tip Function to populate the new array Preserving the previously contents of the array without loosing the previous index. 
It has 3 parameters the old array multidimensional , the new array multidimensional and the number of dimensions.
I'll search in my old Toshiba notebook. 
It could be useful or as input for a better code to make a general purpouse Function Preserving the stored data. 
Thanks for talking about this feature.
Reply




Users browsing this thread: 1 Guest(s)