Computing.Net > Forums > Programming > Array Dimension

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Array Dimension

Reply to Message Icon

Name: donever
Date: March 20, 2009 at 20:29:43 Pacific
OS: windows pro x64
CPU/Ram: athlon 4g
Product: Custom / CUSTOM BUILD
Subcategory: C/C++
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: March 21, 2009 at 03:47:17 Pacific
Reply:

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


0

Response Number 2
Name: Razor2.3
Date: March 21, 2009 at 04:20:00 Pacific
Reply:

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?


0

Response Number 3
Name: StuartS
Date: March 21, 2009 at 05:41:51 Pacific
Reply:

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


0

Response Number 4
Name: Razor2.3
Date: March 22, 2009 at 06:09:43 Pacific
Reply:

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.

0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


adjusting the volume in b... error in senedin mail



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Array Dimension

Graphical 2D Array Display VB.NET www.computing.net/answers/programming/graphical-2d-array-display-vbnet/11062.html

C++ large array size www.computing.net/answers/programming/c-large-array-size/7963.html

assigning multi-dimension arrays www.computing.net/answers/programming/assigning-multidimension-arrays/6333.html