Computer Science Canada

Multi-dimentional Arrays?

Author:  ZeroPaladn [ Tue Dec 05, 2006 12:52 pm ]
Post subject:  Multi-dimentional Arrays?

I was jst wondering how would i do a Multidimentional array, cuse i have no clue about it. I know that a normal array looks like so...
VisualBASIC:
Dim whatever (1 to 10) as Integer

... a multidimentoinal array would look something like that, no?

Thanks for the help guys.

Author:  Silent Avenger [ Tue Dec 05, 2006 4:29 pm ]
Post subject: 

It's called a two dimensional array. It's very similar to the regular array but it has an extra bit put in.

code:
Dim var(1 To 10, 1 To 10) As Integer


That's what a two dimensional array looks like when it's declared. When identifying a location in the array the data in the brackets would have two items eg (2, 3). The easiest way to look at a two dimensional array is to see it as a table (a table as in a chart) with columns and rows. The first number will show which row the item is in and the second number will show which column it is in.

Author:  cool dude [ Tue Dec 05, 2006 5:06 pm ]
Post subject: 

Silent Avenger wrote:
It's called a two dimensional array. It's very similar to the regular array but it has an extra bit put in.

code:
Dim var(1 To 10, 1 To 10) As Integer


That's what a two dimensional array looks like when it's declared. When identifying a location in the array the data in the brackets would have two items eg (2, 3). The easiest way to look at a two dimensional array is to see it as a table (a table as in a chart) with columns and rows. The first number will show which row the item is in and the second number will show which column it is in.


umm. i'm pretty sure he can call it multidimensional. they are pretty much the same thing. a multidimensional array could be a two dimensional but not the other way around. But your code is correct.

code:

Dim matrix(5, 15, 10) As Single

Author:  Silent Avenger [ Tue Dec 05, 2006 6:51 pm ]
Post subject: 

Yeah but my teacher prefers to call it a two dimensional array because we use an array with two subscripts the most. We also like to use user defined types which helps with a lot of things.

Author:  cool dude [ Tue Dec 05, 2006 9:33 pm ]
Post subject: 

Silent Avenger wrote:
Yeah but my teacher prefers to call it a two dimensional array because we use an array with two subscripts the most. We also like to use user defined types which helps with a lot of things.


so what does your teacher call this:
code:

Dim matrix(5, 15, 10) As Single


its not two dimensional. its multidimensional!

Author:  Silent Avenger [ Tue Dec 05, 2006 9:42 pm ]
Post subject: 

Yeah but we've only used an array like that I think once maybe twice in the past 3 years (there's only one compsci teacher in our school because the classes are not that big) and in any of the programs that we make we usually don't need an array with more than 2 dimensions and where we could use one we use a UDT.

Author:  ZeroPaladn [ Wed Dec 06, 2006 12:39 pm ]
Post subject: 

thanks for the help guys, although i was wondering something else now, yknow how you can have a control array (ex. txtDisplay(1), txtDisplay(2), ect.), i was wondering if you could have a multidimentional control array, for like a displaying information on a grid of text boxes, per say?

Author:  NikG [ Wed Dec 06, 2006 1:05 pm ]
Post subject: 

cool dude wrote:
umm. i'm pretty sure he can call it multidimensional. they are pretty much the same thing. a multidimensional array could be a two dimensional but not the other way around.
To clarify: a two-dimensional array IS a multi-dimensional array! But not every multi-dimensional array does not have to be a 2d array because it could have more than 2 dimensions (like cool dude's Dim matrix code).

ZeroPaladn, I don't think you can have multidimensional control arrays because I believe the controls are not actually arrays, they just allow the same name with different index #s, and there is not place for multiple index #s.

Instead, you could have multiple control arrays. I.e:

TextBox1(1) TextBox2(1) TextBox3(1)
TextBox1(2) TextBox2(2) TextBox3(2)
TextBox1(3) TextBox2(3) TextBox3(3)

This way, you have both the row (in the name of the control) and the column (in the index number).

Author:  ZeroPaladn [ Wed Dec 06, 2006 1:15 pm ]
Post subject: 

I never thought of that...

This i the current code using a single control array for the textboxes, and a 2D array for inputing text into the textbox.

VisualBASIC:
For counts = 0 To 15 Step 1
    temp1 = temp1 + 1
    If temp1 = 5 Then
        temp1 = 1
        temp2 = temp2 + 1
    End If
    If temp1 = temp2 Then
    Else
        txtDisplay(counts) = Str$(busPricing(temp1, temp2))
        txtDisplay(15 - counts) = Str$(busPricing(temp2, temp1))
    End If
Next counts

Unfortuantly, i get a array subscript out of range wiht this, and it was pissing me off. Your method of doing it, NikG, help greatly.

Author:  wtd [ Wed Dec 06, 2006 1:27 pm ]
Post subject: 

code:
    If temp1 = temp2 Then
    Else
        txtDisplay(counts) = Str$(busPricing(temp1, temp2))
        txtDisplay(15 - counts) = Str$(busPricing(temp2, temp1))
    End If


Ummm.... why not just the following?

code:
    If temp1 <> temp2 Then
        txtDisplay(counts) = Str$(busPricing(temp1, temp2))
        txtDisplay(15 - counts) = Str$(busPricing(temp2, temp1))
    End If

Author:  Silent Avenger [ Wed Dec 06, 2006 5:54 pm ]
Post subject: 

There are a few things that you can do ZeroPaladn to streamline your code.
One thing you could do is remove the Step 1 in your for loop because the loop automatically counts up by one. Another thing as wtd has already pointed out, instead of the extra Else in your if statement you could fix it wtd's way or this way:
code:
    If Not temp1 = temp2 Then
        txtDisplay(counts) = Str$(busPricing(temp1, temp2))
        txtDisplay(15 - counts) = Str$(busPricing(temp2, temp1))
    End If

As for your subscript out of range error, by the looks of your code it seems that your counters are over counting. Try not to use counters in a for loop because it's easy to make counting errors in a loop like this. If you still can't figure out what's wrong just click on the Debug button on the error message that pops up and it should take you to the problem code and you'll be able to hover your mouse over the variables and get the values.

Author:  Brightguy [ Fri Dec 08, 2006 8:19 pm ]
Post subject:  Re: Multi-dimentional Arrays?

Multidimensional control arrays aren't available, but an easy workaround is to create a mapping from your multidimensional coordinates to the 1-dimensional Index.

Author:  Silent Avenger [ Fri Dec 08, 2006 9:55 pm ]
Post subject: 

Sorry Brightguy, but this has already been mentioned.

NikG wrote:
ZeroPaladn, I don't think you can have multidimensional control arrays because I believe the controls are not actually arrays, they just allow the same name with different index #s, and there is not place for multiple index #s.

Instead, you could have multiple control arrays. I.e:

TextBox1(1) TextBox2(1) TextBox3(1)
TextBox1(2) TextBox2(2) TextBox3(2)
TextBox1(3) TextBox2(3) TextBox3(3)

This way, you have both the row (in the name of the control) and the column (in the index number).

Author:  Brightguy [ Fri Dec 08, 2006 11:48 pm ]
Post subject:  Re: Multi-dimentional Arrays?

Using multiple control arrays is not optimal for a number of reasons, for example even just iterating through every control requires explicitly listing every control array you have (rather than letting a loop handle it).

I was proposing using a single control array to emulate a multidimensional one, by mapping your multidimensional coordinates (I, J) onto Index. The easiest way would be to use Index = I*intColumns + J, where your array is intColumns by intRows.

Author:  Silent Avenger [ Sat Dec 09, 2006 3:39 pm ]
Post subject: 

Sorry I miss took your first post. Anyways doing it the way NikG has showed I think is a much easier way of doing it especially if you're only going to have a few columns, but doing it your way would work also.

Author:  ZeroPaladn [ Tue Dec 12, 2006 1:12 pm ]
Post subject: 

Thanks guys for all your help, i got perfect! (I think i'm getting better no?) A few things though...

Silent Avenger wrote:
One thing you could do is remove the Step 1 in your for loop because the loop automatically counts up by one.

Why remove it? Sure, it doesn't do anything, but I beleive it's good coding practice (thank wtd for drilling that to most people's head with his Turing Code Streamlining thread). Might as well learn that now instead of later. Either way, if i did want to change it, i could just use the replace function to replace Step 1 with lets say... Step 1337, instead of typing it in to every for loop that requires it, its just much quicker.

wtd wrote:
code:
If temp1 = temp2 Then
    Else
        txtDisplay(counts) = Str$(busPricing(temp1, temp2))
        txtDisplay(15 - counts) = Str$(busPricing(temp2, temp1))
    End If



Ummm.... why not just the following?


code:
If temp1 <> temp2 Then
        txtDisplay(counts) = Str$(busPricing(temp1, temp2))
        txtDisplay(15 - counts) = Str$(busPricing(temp2, temp1))
    End If


Heh, never thought of that. Again, either one works, but yours is much more understandable for someone reading the code.


: