|
-
Apr 8th, 2023, 02:14 AM
#1
Thread Starter
Lively Member
Single for-loop traversal over 2D/3D arrays
This source code shows the traversal of a two-dimensional array by one for-loop structure. A 2D-array variable (A) is declared. A string variable t is initially set to empty. A variable v is set to zero and it represents the main counter of the for-loop. Another two variables (ie. i and j) are initialized with value zero and are the main coordinates for element identification. Each dimension of array A is stored in variables n and m, namely the number of rows in n and the number of columns in m. The upper limit of the for-loop is calculated based on the two known dimensions n and m. Thus, m times n establishes the upper limit of the for-loop. Here, the value of the counter v from the for-loop is used to calculate the i and j values that are used as an index to traverse the array variable A. The value of variable j is computed as the v % m and the result of this expression indicates the reminder (ex. 5 mod 3 is 2). The secret to this implementation is a condition that increments a variable i (rows) each time j (columns) equals zero. Thus, in this manner this approach provides the i and j values that a nested for-loop provides. At each iteration, the value from an element is added to the content of variable t. Once the end of the for-loop is reached, the value collected in variable t is printed in the output for inspection.
Please also see: Single "for-loop" traversal over three dimensional arrays.
Code:
Sub main()
Dim A(0 To 1, 0 To 2) As String
Dim i As Integer
Dim t As String
A(0, 0) = "a"
A(0, 1) = "b"
A(0, 2) = "c"
A(1, 0) = "d"
A(1, 1) = "e"
A(1, 2) = "f"
n = UBound(A, 1) - LBound(A, 1) + 1 ' rows
m = UBound(A, 2) - LBound(A, 2) + 1 ' columns
i = 0
j = 0
For v = 0 To (n * m) - 1
j = v Mod m
If (v <> 0 And j = 0) Then i = i + 1
t = t & v & " A(" & i & "," & j & ")="
t = t & A(i, j) & vbCrLf
Next v
Debug.Print (t)
End Sub
Some mirrored examples in different programming languages (including VB6), may also be of interest.
Last edited by Paul A. Gagniuc; Apr 8th, 2023 at 02:27 AM.
-
Apr 8th, 2023, 03:31 PM
#2
Re: Single for-loop traversal over 2D/3D arrays
the "i" variable can be easier derived via integer-division of the last "dimension-count" (m).
Code:
Sub Main()
Dim A(0 To 1, 0 To 2) As String
Dim i As Long, j As Long, k As Long, m As Long
A(0, 0) = "a"
A(0, 1) = "b"
A(0, 2) = "c"
A(1, 0) = "d"
A(1, 1) = "e"
A(1, 2) = "f"
m = UBound(A, 2) - LBound(A, 2) + 1 'we only need 'm' (second dimension)
For k = 0 To m * (UBound(A, 1) - LBound(A, 1) + 1) - 1
i = k \ m
j = k Mod m
Debug.Print k, i, j, A(i, j)
Next
End Sub
This kind of mapping is usually applied,
when you have a one-dimensional Array (a Vector, a List) -
and want to "map-it" to a "2D-Grid".
E.g. "ThumbNail-Rendering" of a "one-dimensional-list" of small images will make a better example...
(showing a use-case, which is more common in daily work):
Code:
Option Explicit
Const ThumbSize = 900
Private TList() As String
Private Sub Form_Load()
FontName = "Arial": FontSize = 36: AutoRedraw = True
TList = Split(" a, b, c, d, e, f, g, h", ",")
Caption = "Resize me!"
End Sub
Private Sub Form_Resize()
Cls
RenderThumbnailsInColumns ScaleWidth \ ThumbSize, TList
End Sub
Sub RenderThumbnailsInColumns(ByVal Columns As Long, TList)
If Columns < 1 Then Columns = 1
Dim k As Long
For k = 0 To UBound(TList)
CurrentY = (k \ Columns) * ThumbSize
CurrentX = (k Mod Columns) * ThumbSize
Line (CurrentX + ThumbSize, CurrentY + ThumbSize)-(CurrentX, CurrentY), vbRed, B
Print TList(k)
Next
End Sub
Olaf
Last edited by Schmidt; Apr 8th, 2023 at 04:02 PM.
-
Apr 8th, 2023, 04:06 PM
#3
Thread Starter
Lively Member
Re: Single for-loop traversal over 2D/3D arrays
 Originally Posted by Schmidt
the "i" variable can be easier derived via integer-division of the last "dimension-count" (m).
Code:
Sub Main()
Dim A(0 To 1, 0 To 2) As String
Dim i As Long, j As Long, k As Long, m As Long
A(0, 0) = "a"
A(0, 1) = "b"
A(0, 2) = "c"
A(1, 0) = "d"
A(1, 1) = "e"
A(1, 2) = "f"
m = UBound(A, 2) - LBound(A, 2) + 1 'we only need 'm' (second dimension)
For k = 0 To m * (UBound(A, 1) - LBound(A, 1) + 1) - 1
i = k \ m
j = k Mod m
Debug.Print k, i, j, A(i, j)
Next
End Sub
This kind of mapping is usually applied,
when you have a one-dimensional Array (a Vector, a List) -
and want to "map-it" to a "2D-Grid".
E.g. "ThumbNail-Rendering" of a "one-dimensional-list" of small images will make a better example...
(showing a use-case, which is more common in daily work):
Code:
Option Explicit
Const ThumbSize = 900
Private TList() As String
Private Sub Form_Load()
FontName = "Arial": FontSize = 36: AutoRedraw = True
TList = Split(" a, b, c, d, e, f, g, h", ",")
Caption = "Resize me!"
End Sub
Private Sub Form_Resize()
Cls
RenderThumbnailsInColumns ScaleWidth \ ThumbSize, TList
End Sub
Sub RenderThumbnailsInColumns(ByVal Columns As Long, TList)
If Columns < 1 Then Columns = 1
Dim k As Long
For k = 0 To UBound(TList)
CurrentY = (k \ Columns) * ThumbSize
CurrentX = (k Mod Columns) * ThumbSize
Line (CurrentX + ThumbSize, CurrentY + ThumbSize)-(CurrentX, CurrentY), vbRed, B
Print TList(k)
Next
End Sub
Olaf
Thank you Olaf, that is super ! Actually that is beautiful and elegant!
-
May 11th, 2026, 01:30 PM
#4
Thread Starter
Lively Member
Re: Single for-loop traversal over 2D/3D arrays
 Originally Posted by Schmidt
the "i" variable can be easier derived via integer-division of the last "dimension-count" (m).
Code:
Sub Main()
Dim A(0 To 1, 0 To 2) As String
Dim i As Long, j As Long, k As Long, m As Long
A(0, 0) = "a"
A(0, 1) = "b"
A(0, 2) = "c"
A(1, 0) = "d"
A(1, 1) = "e"
A(1, 2) = "f"
m = UBound(A, 2) - LBound(A, 2) + 1 'we only need 'm' (second dimension)
For k = 0 To m * (UBound(A, 1) - LBound(A, 1) + 1) - 1
i = k \ m
j = k Mod m
Debug.Print k, i, j, A(i, j)
Next
End Sub
This kind of mapping is usually applied,
when you have a one-dimensional Array (a Vector, a List) -
and want to "map-it" to a "2D-Grid".
E.g. "ThumbNail-Rendering" of a "one-dimensional-list" of small images will make a better example...
(showing a use-case, which is more common in daily work):
Code:
Option Explicit
Const ThumbSize = 900
Private TList() As String
Private Sub Form_Load()
FontName = "Arial": FontSize = 36: AutoRedraw = True
TList = Split(" a, b, c, d, e, f, g, h", ",")
Caption = "Resize me!"
End Sub
Private Sub Form_Resize()
Cls
RenderThumbnailsInColumns ScaleWidth \ ThumbSize, TList
End Sub
Sub RenderThumbnailsInColumns(ByVal Columns As Long, TList)
If Columns < 1 Then Columns = 1
Dim k As Long
For k = 0 To UBound(TList)
CurrentY = (k \ Columns) * ThumbSize
CurrentX = (k Mod Columns) * ThumbSize
Line (CurrentX + ThumbSize, CurrentY + ThumbSize)-(CurrentX, CurrentY), vbRed, B
Print TList(k)
Next
End Sub
Olaf
Hi Olaf, I remembered this exchange/post for a long time and it was inspirational for me, so here are some developments published today:
https://www.mdpi.com/1999-4893/19/5/375
VB6:
Code:
Sub Main()
Dim cube As String, Slices, Rows, Cells
Dim m%, n%, q%, v%, k%, i%, j%, d%
cube = "11,12,13|14,15,16|17,18,19/" & _
"21,22,23|24,25,26|27,28,29/" & _
"31,32,33|34,35,36|37,38,39"
Slices = Split(cube, "/")
Rows = Split(Slices(0), "|")
Cells = Split(Rows(0), ",")
q = UBound(Slices) + 1
m = UBound(Rows) + 1
n = UBound(Cells) + 1
For v = 0 To m * n * q - 1
k = v Mod (m * n)
j = v Mod n
i = (k - j) \ n
d = (v - k) \ (m * n)
Rows = Split(Slices(d), "|")
Cells = Split(Rows(i), ",")
Debug.Print "v="; v; " d="; d; " i="; i; " j="; j; " value="; Cells(j)
Next v
End Sub
or:
https://www.quitebasic.com/
Code:
10 DIM A(26)
20 FOR V = 0 TO 26
30 READ A(V)
40 NEXT V
50 DATA 11,12,13,14,15,16,17,18,19
60 DATA 21,22,23,24,25,26,27,28,29
70 DATA 31,32,33,34,35,36,37,38,39
80 LET M = 3
90 LET N = 3
100 LET S = 3
110 FOR V = 0 TO M * N * S - 1
120 LET K = V - INT(V / (M * N)) * (M * N)
130 LET J = V - INT(V / N) * N
140 LET I = (K - J) / N
150 LET D = (V - K) / (M * N)
160 PRINT "v="; V; " d="; D; " i="; I; " j="; J; " value="; A(V)
170 NEXT V
Best regards,
Paul
Last edited by Paul A. Gagniuc; May 11th, 2026 at 01:40 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|