Code to shuffle and rearrange a given matrix

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Code to rearrange a shuffled matrix to the original form
            Console.Write("Enter number of rows: ");
            string str = Console.ReadLine();
            int rows = 0;
            rows = int.Parse(str);
            Console.Write("Enter number of columns: ");
            str = Console.ReadLine();
            int cols = 0;
            cols = int.Parse(str);
            //Declaring the reservoir matrix
            char[,] matrix = new char[rows, cols];
            Random rand = new Random();
            int k = 0;
            char[] charOriginalArray = new char[rows * cols];
            //Reading the input matrix
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write("Enter Element(" + (i + 1) + "," + (j + 1) + "): ");
                    string digit = Console.ReadLine();
                    matrix[i, j] = char.Parse(digit);
                    charOriginalArray[k] = matrix[i, j];
                    k++;
                }
            }
            //Displaying the input matrix
            Console.WriteLine("Displaying Input Matrix:");
            Console.WriteLine("========================");
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("========================");
            //Shuffling the input matrix
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    char temp = matrix[i, j];
                    int tempI = 0, tempJ = 0;
                    do
                    {
                        tempI = rand.Next(rows);
                        tempJ = rand.Next(cols);
                    } while (tempI == i && tempJ== j);
                    matrix[i, j] = matrix[tempI, tempJ];
                    matrix[tempI, tempJ] = temp;
                }
            }
            //Displaying the shuffled matrix
            Console.WriteLine("Displaying Shuffled Matrix:");
            Console.WriteLine("===========================");
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("===========================");
            //Rearranging the shuffled matrix to the original form
            //Creating a linear array of characters
            char[] charShuffledArray = new char[rows * cols];
            k = 0;
            //Populating the linear array
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    charShuffledArray[k] = matrix[i, j];
                    k++;
                }
            }
            Permutate(charShuffledArray, charOriginalArray, rows, cols);
        }
        //Display Matrix
        public static void Display(char[] charTempArray, int rows, int cols)
        {
            Console.WriteLine("Displaying Rearranged Matrix:");
            Console.WriteLine("=============================");
            for (int i = 0; i < rows*cols; i++)
            {
                Console.Write(charTempArray[i] + " ");
                if ((i + 1) % cols == 0)
                {
                    Console.WriteLine();
                }
            }
            Console.WriteLine("=============================");
        }
        //Compare Matrix
        public static bool Compare(char[] charTempArray, char[] charOriginalArray)
        {
            if (charTempArray.Length == charOriginalArray.Length)
            {
                for (int i = 0; i < charOriginalArray.Length; i++)
                {
                    if (charTempArray[i] != charOriginalArray[i])
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        //Method to permutate through the char array
        public static void Permutate(char[] charArray, char[] charOriginalArray, int rows, int cols)
        {
            //Create an array based on word size
            char[,] charMatrix = new char[charArray.Length, charArray.Length];
            //Initializing character matrix
            for (int i = 0; i < charArray.Length; i++)
            {
                for (int j = 0; j < charArray.Length; j++)
                {
                    charMatrix[i, j] = charArray[j];
                }
            }
            //Create the pointer array
            int[] pointer = new int[charArray.Length];
            //Pointer array initialization
            for (int i = 0; i < charArray.Length; i++)
            {
                pointer[i] = i;
            }
            //Pointer increment loop
            pointer[charArray.Length – 1]–;
            do
            {
                int i = charArray.Length – 1;
            label1:
                pointer[i]++;
                if (pointer[i] == charArray.Length && i != 0)
                {
                    pointer[i] = 0;
                    i–;
                    goto label1;
                }
                else if (pointer[0] == charArray.Length)
                {
                    break;
                }
                else
                {
                    bool valid = true;
                    //Check for valid pointer values
                    int[] check = new int[charArray.Length];
                    for (int j = 0; j < charArray.Length; j++)
                    {
                        check[pointer[j]]++;
                        if (check[pointer[j]] > 1)
                        {
                            valid = false;
                            break;
                        }
                    }
                    if (valid)
                    {
                        char[] charTempArray = new char[rows * cols];
                        //Print the matrix based on pointer array
                        for (int j = 0; j < charArray.Length; j++)
                        {
                            charTempArray[j] = charMatrix[j, pointer[j]];
                        }
                        //Display
                        Display(charTempArray, rows, cols);
                        //Compare
                        if (Compare(charTempArray, charOriginalArray))
                        {
                            Console.WriteLine("Rearrange successful");
                            return;
                        }
                    }
                }
            } while (pointer[0] != charArray.Length);
        }
    }
}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s