Code to shuffle a given matrix

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //Code to shuffle a given matrix

            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

            int[,] matrix = new int[rows, cols];

            Random rand = new Random();

            //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] = int.Parse(digit);

                }

            }

            //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++)

                {

                    int 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("========================");

        }

    }

}

 

Output:
==========================
Enter number of rows: 3
Enter number of columns: 3
Enter Element(1,1): 1
Enter Element(1,2): 2
Enter Element(1,3): 3
Enter Element(2,1): 4
Enter Element(2,2): 5
Enter Element(2,3): 6
Enter Element(3,1): 7
Enter Element(3,2): 8
Enter Element(3,3): 9
Displaying Input Matrix:
========================
1 2 3
4 5 6
7 8 9
========================
Displaying Shuffled Matrix:
===========================
5 7 2
3 6 9
4 1 8
========================
Press any key to continue . . .

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