THE BORE
General => The Superdeep Borehole => Topic started by: T234 on May 01, 2008, 12:40:54 AM
-
FUK U VISUAL BASIC IMMA GOIN HOME AND PLAYIN GTA
-
making a matrix in vb is fucking easy.
-
two dimensional arrays are easy bud. Just think of it as a coordinate system.
1 2 3 4 5
1[F][ ][O][ ][ ]
2[ ][ ][ ][ ][C]
3[Y][ ][U][ ][ ]
4[ ][ ][ ][U][ ]
5[ ][ ][K][ ][ ]
Then your array at:
(1,1) is F
(3,3) is U
(5,2) is C
(3,5) is K
(1,3) is Y
(3,1) is O
(4,4) is U
from that point on it's just a matter of the syntax of the language you're working with.
You can iterate through all items in the array using 2 for loops as follows:
Array ass(5,5) (a 5x5 array)
Integer val
For i=0 to 4
For j=0 to 4
val=val+ass(i,j)
Next (the next j)
Next (the next i)
A more visual representation of that is as follows:
val=(0,0)+(0,1)+(0,2)+(0,3)+(0,4)+(1,0)+(1,1)+(1,2)+(1,3)+(1,4)+(2,0)+(2,1)+(2,2)+(2,3)+(2,4)+.....
Make sense? I did a lot of shitty pseudocode there, but hopefully it helps a bit.
-
Ecro :bow2
for actually helping and not telling him how fucking easy they are.
-
I just noticed that my first array is 1-based, and my second is 0-based. Don't let that throw you for a loop, that's just me being a jerk.
Anyway, I think VB does 1-based arrays, from what I recall. I also remember that when Dim'ing an array in VB, if you give it an actual size ( something like Dim ass as Integer(5,5), or however the syntax is), it doesn't actually create an array with 5x5 elements, but uses that 5 as the highest index, which causes things to be completely fucking screwy.
I'm gonna consult the MSDN right now to make sure I'm not leading you astray.
-
:lol @ example
I'm going to be making a lot of these threads during the summer.
-
I thought vb arrays started at zero. I always remember it pissing me off so I had to right a little line of code at the top to make it stop fucking with me.
-
OK, I just consulted the MSDN.
Here's the skinny:
It looks like Arrays in VB are now zero-based (I'm pretty sure they weren't in VB6, but that was years ago). What that means, if you're not sure, is that all your counting begins at 0. For instance, if you wanted to find the first item in the array foo, you'd look at foo(0).
VB has one major flaw when it comes to arrays, IMO, and it's likely to screw you up.
If you want to create an integer array with 10 elements, you'd do this:
Dim foo(9) as Integer
When creating arrays of a specific size in VB, the number in parenthesis is the highest element you want for your arrays. So the code above creates an array called foo with indexes 0,1,2,3,4,5,6,7,8,9 (which total to 10 indexes). This is different from like every other language I've programmed in, because everything else I've used has you create arrays based on the number of elements in the array, and NOT the highest index. I won't go into this much, though, as it's likely to just confuse you.
So, again, when you're iterating through your array, begin with 0, and work until the end, as follows:
Dim foo(4) as Integer
dim total as Integer
'for the sake of illustration, let's assume foo has values as follows:
'foo(0)=1
'foo(1)=2
'foo(2)=3
'foo(3)=4
'foo(4)=5
total = 0
For I = 0 to 4
total = total + foo(i)
Next
MessageBox.Show(total.ToString())
When the code works through itself, it essentially does:
total=0+foo(0) '0+1
total=1+foo(1) '1+2
total=3+foo(2) '3+3
total=6+foo(3) '6+4
total=10+foo(4) '10+5
which adds up 10 15.
Now, multi-dimensional arrays are essentially the same exact thing, except you're making your array a grid instead of just a single row of data.
So, in the above case that Dim foo(4) as Integer would result in something that visually would look like this:
0 1 2 3 4
0[ ][ ][ ][ ][ ]
A multi-dimensional array is very similar, but it expands the whole thin so it's like a grid, as follows:
Dim foo(4,4) as Integer
which visually looks like this
0 1 2 3 4
0[ ][ ][ ][ ][ ]
1[ ][ ][ ][ ][ ]
2[ ][ ][ ][ ][ ]
3[ ][ ][ ][ ][ ]
4[ ][ ][ ][ ][ ]
So when you want to access that, data, pretend you're playing Battleship. You need to know both the row and column of the data you're after so that you can access it.
for instance:
Dim foo(4,4) as Integer
Dim value as Integer
...
value = foo(2,4)
And again, if you need to iterate through everything in a multi-dimensional array, you can next For loops like I did in my above post.
Does this help to clear anything up at all?
-
I had to SEARCH through an two-dimensional array for the highest value, and return what row it was in. I was running on no sleep, so I just said fuck it and and went home.
I got a 90 on the VB final exam today. :)
-
Again, I'd do that with 2 For-Next loops.
Dim foo(4,4) as Integer
Dim highestValue as Integer
Dim rowLocation as Integer
...fill in foo...
highestValue=foo(0,0)
rowLocation=0
For i = 0 to 4
For j = 0 to 4
If foo(i,j)>highestValue Then
highestValue=foo(i,j)
rowLocation=i
End if
Next j
Next i
tsc tsc
Instead of using hard-coded maximums and whatnot, you could also do something like
For i = 0 to foo.GetLength(0)-1
For j = 0 to foo.GetLength(1)-1
blah
Next j
Next i
Though I'm not sure exactly of the exact syntax in VB for doing methods.
The way that the GetLength() function works, is that it returns the number of elements in the specified dimension of the array.
-
two dimensional arrays are easy bud. Just think of it as a coordinate system.
1 2 3 4 5
1[F][ ][O][ ][ ]
2[ ][ ][ ][ ][C]
3[Y][ ][U][ ][ ]
4[ ][ ][ ][U][ ]
5[ ][ ][K][ ][ ]
Then your array at:
(1,1) is F
(3,3) is U
(5,2) is C
(3,5) is K
(1,3) is Y
(3,1) is O
(4,4) is U
:rofl