|
Author Topic:   C# arrays and doing searches
kv
Member
posted April 26, 2004 05:00 PM            
ok, have a small programming problem that's been driving me nuts.

I have a two dimensional array, within this array exists an element that is not anywhere else in the array.

According to my book, C# provides a method to find it if it were in a one dimensional array, but gives no suggestions on how to find it in a two dimensional.

I can find it the hard way, but does C# provide a method to do it?

IP:

KiLlEr
Member
posted April 26, 2004 07:48 PM            
not directly, but you can create an 'n' dimentional array class by inheriting from a single dimensional array and override the search method with a recursive method that simply calls itself until it hits an array entry that contains an array of a single dimension, then calls the base class's search method, instead of the override.

IP:

kv
Member
posted April 26, 2004 11:50 PM            
... that sounds more complicated then what I thought was the hard way.

int x;
int y;

for loop on x
nested for loop on y
check to see if array[x,y] = (thing i'm looking for), break from the loop if it is.

IP:

KiLlEr
Member
posted April 27, 2004 01:18 PM            
It isn't that much more complicated, but its only worth doing if you want it to override the search method.

Also, instead of a nested loop, use a single loop and call the find method for each subarray. Then you only need to worry about exiting one loop.

Of course this depends on the type of array your working with, since C# supports both jagged/staggard and rectangular/matrix multidimensional array types.

[This message has been edited by KiLlEr (edited April 27, 2004).]

IP:

kv
Member
posted April 27, 2004 02:06 PM            
it's a simple 8x8 array. I just need to test some conditions on a unique element of the array, and to do that, I need to know where it is.

I figured out that I can break out of the x loop by testing the value of y, as long as I declare both variables outside of the loops.

IP:

kv
Member
posted April 27, 2004 06:58 PM            
yay! it works!

so why is the program crashing when I do a specific thing???

oh... maybe that's why...

helps if I get array[x,y] and not array[x,(other x)]

IP: