Array in an array
#31
(02-21-2023, 01:18 AM)Kernelpanic Wrote:
Quote:@KernelPanic

I  do not know how and why I missed my 2 different answers to your post!

Forget it! What do you think I have all missed in my life. . . Take it easy!  Tongue


Hi kernelpanic
Not you 
I was referring to my attempt to post an answer to you. 
First my two attempt are gone into void
Reply
#32
(02-21-2023, 02:26 PM)TempodiBasic Wrote: DIM myArray (1 to 4) AS INTEGER ' this is static
DIM my2ndArray (1 to M)  AS INTEGER' This is dynamic and M =0

Very good catch!

However variable M cannot be zero if there should be a "1 TO" or any value at all higher than one.

That doesn't seem to be indicated by the QB64 Wiki (I don't sleep well lately so my eyes are tired now):

https://qb64phoenix.com/qb64wiki/index.php/DIM

That older site linked to earlier only shows examples that worked in GW-BASIC, it must have been around to convert those old programs to run in QBasic...

EDIT: Agree with you that this line should have been flagged by the IDE as an error:

Code: (Select All)
DIM AS Test xyz(1 TO 10, abc())

However I think that was presented as fake code by the OP.
Reply
#33
Two examples for a dynamic and a static two-dimensional field.

A static field is reinitialized with 0 after erase and can then be filled with new values again. However, it cannot be redimensioned like a dynamic field. So, 10 * 10 cannot become an 8 * 8 field.

Dynamic array with Redim:
Code: (Select All)
'Zweidimensionales Feld mit Redim neu dimensionieren - 29. Dez. 2022

$Console:Only
Option _Explicit

Option Base 1

Dim As Integer neuDimensionZeile, neuDimensionSpalte
Dim As Integer zeilenDim, spaltenDim
Dim As Integer a, b, i, j, y, z

Locate 2, 2
Input "Feldimension Zeilen  : ", zeilenDim
Locate 3, 2
Input "Felddimension Spalten: ", spaltenDim
Dim As Integer zweiDimfeld(zeilenDim, spaltenDim)

Locate CsrLin + 2, 2
z = 1
For i = 1 To zeilenDim
  For j = 1 To spaltenDim
    zweiDimfeld(i, j) = z
    Print Using "## "; zweiDimfeld(i, j),
    z = z + 1
  Next
  Print: Locate , 2
Next

'Vor Neudimensionierung Speicher freigeben. Ist bei Anwendung
'von REDIM nicht noetig, da dieser ERASE + DIM zusammenfasst - S.188
'Erase zweiDimfeld

'Feld neu dimensionieren
Locate CsrLin + 2, 2
Input "Neue Feldimension Zeile : ", neuDimensionZeile
Locate CsrLin + 0, 2
Input "Neue Feldimension Spalte: ", neuDimensionSpalte

ReDim zweiDimfeld(neuDimensionZeile, neuDimensionSpalte)

Locate CsrLin + 2, 2
y = 1
For a = 1 To neuDimensionZeile
  For b = 1 To neuDimensionSpalte
    zweiDimfeld(a, b) = y
    Print Using "## "; zweiDimfeld(a, b),
    y = y + 1
  Next
  Print: Locate , 2
Next

Locate CsrLin + 3, 2

End

Static array with Erase:
Code: (Select All)
'Statisches Array. Keine Redimensionierung moeglich - 21. Feb. 2023

$Console:Only
Option _Explicit

Option Base 1

Const Elemente = 10
Dim As Integer Feld(Elemente, Elemente)

Dim As Integer i, j, z


Locate CsrLin + 2, 2
z = 1
For i = 1 To 10
  For j = 1 To 10
    Feld(i, j) = z
    Print Using "### "; Feld(i, j),
    z = z + 1
  Next
  Print: Locate , 2
Next

Locate CsrLin + 2, 2
Print "Die Werte im Feld existieren noch."

'Das Array existiert noch!
Locate CsrLin + 2, 2
z = 1
For i = 1 To 10
  For j = 1 To 10
    Print Using "### "; Feld(i, j),
    z = z + 1
  Next
  Print: Locate , 2
Next

'Statische Arrays werden nach ERASE neu initialisiert -> 0, S.188
Erase Feld

Locate CsrLin + 2, 2
Print "Das Feld wurde mit ERASE neu initialisiert -> 0."

'Jetzt ist das statische Array neu initialisiert mit 0,
'da keine neuen Werte eingegeben wurden.
Locate CsrLin + 2, 2
z = 1
For i = 1 To 10
  For j = 1 To 10
    Print Using "### "; Feld(i, j),
    z = z + 1
  Next
  Print: Locate , 2
Next

'Only for: Press any key
Locate CsrLin + 3, 2

End
Reply
#34
(02-21-2023, 02:16 PM)bplus Wrote: @mnrvovrfc

ERASE is correct for Static array.

The easy answer for Dynamic array is to just REDIM it again.

Erase can also be used for dynamic arrays, but it is redundant there as Redim includes Dim & Erase.  Wink
Reply
#35
(02-21-2023, 06:04 PM)Kernelpanic Wrote:
(02-21-2023, 02:16 PM)bplus Wrote: @mnrvovrfc

ERASE is correct for Static array.

The easy answer for Dynamic array is to just REDIM it again.

Erase can also be used for dynamic arrays, but it is redundant there as Redim includes Dim & Erase.  Wink

No!

   
   
b = b + ...
Reply
#36
(02-21-2023, 02:26 PM)TempodiBasic Wrote:
(02-21-2023, 01:07 AM)bplus Wrote: Nope! It is not about using variables for lbound or ubound.

REDIM MyDynamicArray(... )  ' this is dynamic

DIM MyStaticArray(... ) ' this is static

Quiz: How do you reset all values to 0 (or "") in Static Array, how do you do it with Dynamic Array?
Hi dear 
Please read #23
Or at this link
Dim & Redim

DIM myArray (1 to 4) AS INTEGER ' this is static
DIM my2ndArray (1 to M)  AS INTEGER' This is dynamic and M =0


IMHO
Dynamic Array
When you change dimensions of array it is restored to default value (0 or "")


This is a QB64 forum not QBasic here is Wiki for QB64:
   
   
b = b + ...
Reply
#37
@TempodiBasic and @Kernelpanic

You can't do this either:
Code: (Select All)
Dim a(1 To 20) ' a is static
For i = 1 To 20
    a(i) = i
Next
ReDim a(0 To 30)
For i = 0 To 30
    Print a(i)
Next
   

When I first started doing QB64 this difference between Static arrays from DIM and Dynamic arrays from REDIM had me quite frustrated until I found ERASE was needed to clear a Static array.

Please don't you guys mess up other people new to QB64  Angry
b = b + ...
Reply
#38
Quote:No!

But! You have misconceptions.
Dynamic Arrays: After running Erase on dynamic arrays, the array is no longer known. It does not exist anymore. That's exactly what your screenshot shows.
Redim includes both! Erase is first applied, then resized with Dim. QBasic, Page 188

Static: Add a Print to the first FOR loop and you'll see the values. After Erase, the array is reinitialized -> 0. Completely correct.
Reply
#39
(02-21-2023, 06:45 PM)Kernelpanic Wrote:
Quote:No!

But! You have misconceptions.
Dynamic Arrays: After running Erase on dynamic arrays, the array is no longer known. It does not exist anymore. That's exactly what your screenshot shows.
Redim includes both! Erase is first applied, then resized with Dim. QBasic, Page 188

Static: Add a Print to the first FOR loop and you'll see the values. After Erase, the array is reinitialized -> 0. Completely correct.

You can't run erase on a Dynamic array!

I erased the Static array and showed it still existed but all the values cleared.

Erase doesn't delete the array!
Update: well I guess Erase does delete a dynamic array because that's what it says in Wiki for ERASE. Seems like an extra step if you want to clear all values.
b = b + ...
Reply
#40
To wrap it up, I meant it like this:

Code: (Select All)
Dim a(1 To 20)
Dim As Integer z

z = 1
For i = 1 To 20
  a(i) = z
  Print Using "## "; a(i);
  z = z + 1
Next

Print: Print
Erase a

For i = 1 To 20
  Print Using "##"; a(i);
Next

[Image: Erase-Static2023-02-21-215735.jpg]

Erase only makes sense with dynamic arrays if one really want to delete the array - for whatever reason.

QBasic is QuickBasic without the ability to create exe files. I have both the QuickBasic 4.5 manuals and the QBasic technical reference book. No difference in the matter!
Reply




Users browsing this thread: 6 Guest(s)