Hi guys,
I have this code to mix elements in an array. Basically I want it to exchange the 1st element with the other elements in order. When it finishes exchanging, it will then take the original array and go to the 2nd element and do the same job as stated. The problem is that it doesn't mix, but duplicate the elements.
public static readonly char[] alphabet = { 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I' , 'K',
'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'};
private void Swap()
{
char[,] grid = new char[5, 5];
grid = alphabet;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
//tempChar = grid[i, j];
if (k == 4)
{
int kIndex = 0;
tempChar = grid[i, j];
grid[i, j] = grid[j, kIndex];
grid[i, kIndex] = tempChar;
}
else
{
tempChar = grid[i, j];
grid[i, j] = grid[i, k++];
grid[i, k++] = tempChar;
}
for (int m = 0; m < 5; m++)
{
for (int n = 0; n < 5; n++)
Console.Write(grid[m, n] + " ");
Console.WriteLine();
}
Console.WriteLine();
}
}
}
}