Array Dimension

Custom / CUSTOM BUILD
March 20, 2009 at 20:29:43
Specs: windows pro x64, athlon 4g
hello! this is in VB. Is one dimensional and 2 dimensional array the same except that the 2 dimensional have 2 subscripts

for example, one dimensional array
dim Array1(1) as integer
Array(0) = 20
Array(1) = 23

2 dimensional Array
dim Array2(1,1) as integer
Array2(0,0) = 1
Array2(0,1) = 3
Array2(1,0) = 34
Array2(1,1) = 23

I think the array is the same except the index is different. If so, why bother with 2 dimensional array when one dimensional could hold the same information.


See More: Array Dimension

Report •


#1
March 21, 2009 at 03:47:17
You are missing th point of a two dimension array. In fact a two dimension array can hold twice the information of a one dimension array.

Imagine you are programming a chess game. You need to map the board which is a two dimension object, eight squares in one direction and eight squares in the other direction, length and breadth. You would define an array of 7 x 7. That way you can address any of the 64 squares in the board. You can't do that with a single dimension array.

If you wished to map a three dimensional object like a Rubik's cube you would the need a three dimension array, length, breadth and depth.

Multidimensional arrays come in very handy when you are programming a data processing application. It makes data handling a whole lot simpler as one dimension can be the number of fields in a record and the other dimension can be the number of records in the record set.

Bear in mind though that a two dimensional array uses more memory than a single dimension array of equivalent size. e.g a 10 element two dimension array will use more memory than two 10 element single dimension arrays.

Stuart


Report •

#2
March 21, 2009 at 04:20:00
StuartS: That way you can address any of the 64 squares in the board. You can't do that with a single dimension array.
Dim squares(63) As Square

StuartS: a 10 element two dimension array will use more memory than two 10 element single dimension arrays.
I'm not convinced a single 10 x 2 array would take up any more room than two 10 x 1 arrays. Do you have any information to back this up?


Report •

#3
March 21, 2009 at 05:41:51
You could use a single dimension array but it would be more difficult to manipulate unless Square is a pre-defined data type I have not come across before. If that is the case I would also expect there to be a Cube data type as well.

In this case I would define the array as Dim squares(7,7) As Integer

Empirical evidence has shown that that a multi-dimensional takes up more memory than the equivalent single dimension array - at least in VB it does.

Stuart


Report •

Related Solutions

#4
March 22, 2009 at 06:09:43
StuartS: Empirical evidence...
...Means nothing. But I don't have anything supporting my position. Curiosity demands an answer, however, so let's find out!

Since there are Fifty-QuaBillion different versions of Basic, let's talk about the versions I have readily available: VB.NET 2K8 and VBScript.

The setup:
So we'll compare each version's private bytes, and since I'm already using VBScript, I might as well write a WMI script. Why? Partly because WMI won't round the numbers, but largely because text is easier to copy/paste.

The script in question (PrivateBytes.vbs):

Set WMI = GetObject("winmgmts:")
For Each a In WScript.Arguments
  PID = 0
  If WMI.Get("Win32_Process").Create(a, Null, Null, PID) Then
    WScript.Echo "Process start failed."
    WScript.Quit 1
  End If

  With WMI.ExecNotificationQuery("SELECT * " & _
                                 "FROM __instancedeletionevent " & _
                                 "WITHIN 1 " & _
                                 "WHERE TargetInstance ISA " & _
                                 "'Win32_PerfFormattedData_PerfProc_Process'" & _
                                 "AND TargetInstance.IDProcess = " & PID)
    With .NextEvent.TargetInstance
      WSCript.Echo "Private bytes for " & a & _
        " (PID " & PID & "): " & _
        FormatNumber(.PrivateBytes, 0) & " B"
    End With
  End With
Next 'a
Q&D, but it gets the job done. This'll run a program, then return the program's private bytes on exit.

VB.NET:
Let's make two versions of the same program. One using a one dimensional array, the other two.

1D.EXE

Module Module1
    Sub Main()
        Dim i As Integer, j As Integer
        Dim a(9999) As Integer
        For i = 0 To 9999
            a(i) = i
        Next
        System.Threading.Thread.Sleep(30000)
    End Sub
End Module

2D.EXE

Module Module1
    Sub Main()
        Dim i As Integer, j As Integer
        Dim a(99, 99) As Integer
        For i = 0 To 99
            For j = 0 To 99
                a(i, j) = i * 100 + j
            Next
        Next
        System.Threading.Thread.Sleep(30000)
    End Sub
End Module

The results?

E:\1>PrivateBytes.vbs e:\1\1D.exe e:\1\2D.exe
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Private bytes for e:\1\1D.exe (PID 4776): 7,000,064 B
Private bytes for e:\1\2D.exe (PID 3720): 7,000,064 B


Sun 03/22/2009  8.56.14.06 +
E:\1>

So there you go. No size difference

VBScript:
As VBS is a scripting language and not a programming language, the size might very well be different. After all, who really knows what goes on in that VBS engine? Also note I cut the wait down to 5 seconds, because 30 was really just too long.

1D.VBS

Dim i, j
Dim a(9999)
For i = 0 To 9999
  a(i) = i
Next 'i
WScript.Sleep 5000

2D.VBS

Dim i, j
Dim a(99,99)
For i = 0 To 99
  For j = 0 To 99
    a(i, j) = i * 100 + j
  Next 'j
Next 'i
WScript.Sleep 5000

Results!

E:\1>PrivateBytes.vbs "wscript e:\1\1D.vbs" "wscript e:\1\2D.vbs"
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Private bytes for wscript e:\1\1D.vbs (PID 5316): 2,899,968 B
Private bytes for wscript e:\1\2D.vbs (PID 4532): 2,899,968 B


Sun 03/22/2009  9.00.43.05 +
E:\1>
So there you go. Our two dimensional syntactical sugar won't eat up our memory.

Report •


Ask Question