Member Avatar for chipbu

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();
                    }

                    

                }
            }
}

Interesting question. Took me about 30 minutes to get this done.

Here's the code:

private char[,] Swap(char[] array)
        {
            // Make a list from the given char[] array
            List<char> fromArray = new List<char>();
            foreach(char chr in array)
                fromArray.Add(chr);

            // This list will hold the new shuffled array
            List<char> randomList = new List<char>();

            // Mixing the array elements
            Random r = new Random();
            int randomIndex = 0;
            while (fromArray.Count > 0)
            {
                randomIndex = r.Next(0, fromArray.Count);
                randomList.Add(fromArray[randomIndex]);
                fromArray.RemoveAt(randomIndex);
            }

            // Converting the list "randomList" to char[,]

            char[,] grid = new char[5, 5];
            int index = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    grid[i, j] = randomList[index];
                    index++;
                }
            }

            return grid;

        }

Example use:

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'};

char[,] grid = Swap(alphabet);

// Test

for (int i = 0; i < 5; i++)
{
      for (int j = 0; j < 5; j++)
     {
            Console.Write(grid[i, j] + ", ");
     }
}

Hope that helps.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.