Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quesiton on Dimensioning an Array
#1
Where I have 3 different arrays with the following Dimensions

Dim Array1(1 To 0)
Dim Array2(1 To 1)
Dim Array3(0 To 1)

Is that middle Array2 the same as Dim Array2(0) or would it be the same as Dim Array2(1)? 
Thanks
Reply
#2
(02-05-2024, 04:30 PM)Dimster Wrote: Where I have 3 different arrays with the following Dimensions

Dim Array1(1 To 0)
Dim Array2(1 To 1)
Dim Array3(0 To 1)

Is that middle Array2 the same as Dim Array2(0) or would it be the same as Dim Array2(1)? 
Thanks

No and No

try this, take lbound and ubound for each array up there and see the difference!
b = b + ...
Reply
#3
Not sure I'm seeing a difference. When I print the upper and lower bounds I'm getting exactly the boundaries I Dimensioned in the arrays. The IDE allows the dimensioning of Array2(1 to 1) and it appears I'm getting the same result if it was simply Dimensioned Array2(1). Here an example:

Dim Array1(1 To 0)
Dim Array2(1 To 1)
Dim Array3(0 To 1)

Print LBound(Array1)
Print UBound(Array1)
Print
Print LBound(Array2)
Print UBound(Array2)
Print
Print LBound(Array3)
Print UBound(Array3)
Print
Print
x = 5
y = 9
z = x + y
For i = 1 To 10
    For ii = 1 To 15
        If i = 1 And ii = 1 Then
            Array2(i) = x + y
            Print Array2(ii)
        End If
    Next
Next

Print
Array2(1) = z
Print
Print Array2(1)

In my actual program I am working with a re-dimensional array ... so something like Array2(1 to n). The thing with n though it is coming up with values of zero and 1, so I'm trying to come up with code to deal with this eventuality. I'm thinking along the lines of a Select Case to capture n. The zero is crashing but (1 to 1) seems to be accepted.
Reply
#4
@Dimster, you can't use this. . . 

Code: (Select All)

Dim Array1(1 To 0)
Dim Array2(1 To 1)
Dim Array3(0 To 1)

Print LBound(Array1)
Print UBound(Array1)
Print
Print LBound(Array2)
Print UBound(Array2)
Print
Print LBound( Array3)
Drucken UBound(Array3)
Drucken
Drucken
x = 5
y = 9
z = x + y
Für i = 1 bis 10
    für ii = 1 bis 15
        Wenn i = 1 und ii = 1, dann
            Array2(i) = x + y
            Drucken Array2(ii)
        End If
    Next
Next

Print
Array2(1) = z
Print
Print Array2(1) 

What you are doing is incomprehensible. If you want a three-dimensional array, then you have to do it like this:

Code: (Select All)

'Dreidimensionales Feld mit graphischer Darstellung - 3. Jan. 2023

$Console:Only
Option _Explicit

Option Base 1
Dim As Integer dreiDimFeld(3, 4, 4)

'"dm" legt die Dimension(Ebenen) fest. Hier dreimal Bloecke a 16
'dz ist Anzahl Zeilen, ds ist Anzahl Spalten
Dim As Integer dm, dz, ds, dFeld
Dim As Integer ebene, zeile, spalte

Locate 2, 2

'Der Ablauf ist: 1te Ebene -> Durchlauf Zeile * Spalte
'dann folgt die naechste Ebene usw. so viele Ebenen
'wie vorhanden sind
dFeld = 1
For dm = 1 To 3
  For dz = 1 To 4
    'Nach jedem sechszehner Block Absatz
    'fuer naechsten Block. Csrlin+1 statt 2 -> schraege Anzeige
    Locate CsrLin + 1, CsrLin + 1
    For ds = 1 To 4
      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
Reply
#5
(02-05-2024, 04:30 PM)Dimster Wrote: Where I have 3 different arrays with the following Dimensions

Dim Array1(1 To 0)
Dim Array2(1 To 1)
Dim Array3(0 To 1)

Is that middle Array2 the same as Dim Array2(0) or would it be the same as Dim Array2(1)? 
Thanks

Let's go over your 3 cases one-by-one, and change them slightly to something else which is easir to understand:

FOR Array1 = 1 TO 0
FOR Array2 = 1 TO 1
FOR Array3 = 0 TO 1

Now, with those FOR statements, what do you expect is going to happen?

FOR Array1 = 1 to 0   <-- This one isn't going to do anything as the lowerbound is already higher than the upperbound.  The moment it runs, it's just going to kick out and ignore that FOR loop.

FOR Array2 = 1 TO 1  <-- This loop is going to start at a value of 1, and stop at a value of one.  It's going to do whatever it's going to do once, and that's it.

FOR Array3 = 0 TO 1  <-- This loop is going to start at a value of 0 and stop at a value of one.  It's going to do whatever it does twice.



Your arrays are more-or-less exactly the same.

DIM Array(1 TO 0)   <-- The lower bound is greater than the higher bound.  You're ending reserving memory for it, before you even start.  This ***IS NOT*** going to run well, nor work very good for you.   (From what I recall, it causes a seg fault at run time perhaps?  Or did we fix that at some point so that it tosses an error in the IDE now?  I'd have to test to check to be certain -- either way, it's not something that I'd count on working at all.)

DIM Array2(1 TO 1)  <-- This is going to create an array in memory called Array2, and it's going to save 1 element for it, and that element index is going to start at 1 and end at 1.   The *ONLY* real element this array has is Array2(1).   If you try Array2(0) or Array2(2), or anything else, you'll get Subscript Out Of Range ERROR.

Dim Array3(0 TO 1)  <-- This is going to create an array in memory called Array3, and it's going to have 2 elements in it, starting at element 0 and ending at element 1.  You can access it via Array3(0) and Array3(1).  Anything besides those two indexes are going to be Subscript Out Of Range Errors.

So for your question of, " Is that middle Array2 the same as Dim Array2(0) or would it be the same as Dim Array2(1)? ", the answer is:

NEITHER!!

Dim Array2(0) would be the same as DIM Array2(0 TO 0) and create a single element array named Array2, but it'd only have one element to it with an index of 0.  If you try to use Array2(1), it'd cause a Subscript Out of Range ERROR.

Dim Array2(1) would be the same as DIM Array2(0 TO 1) and create an array with 2 elements, which are referenced with indexes starting at 0 and ending at 1.

Neither of which is the same as an array with only one index starting at one and ending at one.

Think of it as:

FOR i = 0 TO 0 
FOR i = 1 TO 1 
FOR i = 0 TO 1  

Now, of the first and the last, which one matches the 2nd?    Your answer to the above would be the same with:

 (Dim Array2(0))
 (Dim Array2(1 TO 1))
 (Dim Array2(1))

Which of those match the middle one?
Reply
#6
Thanks all, very helpful. Error trapping ideas abound. (so Lbound = lower: Ubound = upper: and Abound = thedimsterboundfix).
Reply




Users browsing this thread: 1 Guest(s)