Diego
Diego

Reputation: 2362

c# find characters in a two-dimensional array

I have and Two dimensional array like this.

[A] [B] [C]
[D] [E] [F]
[G] [H] [I]

And I want a function that receive a string as parameter and return an array int[,] with a position of each Word of that string.

public int[,] GetPosition(string Word)
{
    int[,] coordenadas = new int[1, Word.Length];
    for (int value = 0; value < Word.Length; value++)
    {
        char letra = Word[value];
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (array[i, j].Equals(letra.ToString()))
                {
                    coordenadas[0, j] = //??
                }
            }
        }
    }

Then I call that function with a Word like GetPosition("GEI")

It has to return an array {{3,1},{2,2},{3,3}}

How can I build an int[,] with every position?

Upvotes: 0

Views: 651

Answers (1)

D Stanley
D Stanley

Reputation: 152556

How can I build an int[,] with every position?

You don't want an int[,], you just want a vector (1-D array) where each value is set of two numbers. You could use a Tuple<int, int> or just a plain int[] or some other structure, depending on how you want to use the data..

So then to set the value in the array, you could do:

int[][] coordenadas= new int[][Word.Length];
...
    coordenadas[value] = new int[] {i, j};

Note that coordenadas does not seem to need to be a 2-D array.

Upvotes: 3

Related Questions