# Set using array in C #

Posted on

#### Question :

I have the following array:

```a   d   k   m
s   j   e   r
t   c   f   p
```

I put such array in an array as follows:

``````char[,] matriz = new char[3,4];
matriz[0,0] = 'a';
matriz[0,1] = 'd';
matriz[0,2] = 'k';
matriz[0,3] = 'm';
matriz[1,0] = 's';
// E assim sucessivamente.
``````

I would like to generate all possible sets based on such array, taking into account quantity of columns to determine the size of the set.
In the above example, it would be possible to create (3 ^ 4) 81 different sets.

``````Ex.:
``````

What would be the best way to wipe this array and create sets based on the columns?

``````int qtdColunas = 4;

char[] conjunto1 = new char[qtdColunas];
conjunto1 = 'a';
conjunto1 = 'd';
conjunto1 = 'k';
conjunto1 = 'm';

char[] conjunto2 = new char[qtdColunas];
conjunto1 = 'a';
conjunto1 = 'd';
conjunto1 = 'k';
conjunto1 = 'r';

char[] conjunto3 = new char[qtdColunas];
conjunto1 = 'a';
conjunto1 = 'd';
conjunto1 = 'k';
conjunto1 = 'p';
``````

I tried to find something on the web already, but I can not exactly name what I’m doing.
Home
I would like a clean, fast and low memory consumption solution.
Home
The matrix given in the question is just an example, because in my code I can construct larger arrays. And of course I do not fill it line by line as exemplified.
Home
I think it’s just a matter of logic, but good practice makes a lot of difference at the moment. By beginning, I would not want to make any more mistakes than acceptable.

I imagine this will resolve any array size

``````    public static string[] TodosPossiveis(char[,] letras)
{
int linhas = letras.GetUpperBound(0) + 1;
int colunas = letras.GetUpperBound(1) + 1;
int max = (int)Math.Pow(linhas, colunas);
string[] todos = new string[max];

int[] posY = new int[colunas];

int atual = 0;
while(atual < max)
{
string nova = "";
for(int i = 0; i < colunas; i++)
{
nova += letras[posY[i], i];
}
for(int i = colunas-1; i > -1; i--)
{
posY[i]++;
if(posY[i] == linhas)
{
posY[i] = 0;
}
else
{
break;
}
}
todos[atual++] = nova;
}