Rotate a square matrix by 90 degrees clockwise

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int[,] inputSquareMatrix = {
                                               { 1, 2, 3, 5, 6 },
                                               { 1, 2, 3, 5, 6 },
                                               { 1, 2, 3, 5, 6 },
                                               { 1, 2, 3, 5, 6 }, 
                                               { 1, 2, 3, 5, 6 }
                                           };
                PrintMatrix(inputSquareMatrix);
                //Rotate clockwise all the layers
                for (int layer = 0; layer < inputSquareMatrix.GetLength(0) / 2; layer++)
                {
                    //Repeat with all the elements in a layer
                    for (int i = layer; i < inputSquareMatrix.GetLength(0) - 1 - layer; i++)
                    {
                        int temp = inputSquareMatrix[layer, i];
                        //left to top
                        inputSquareMatrix[layer, i] = inputSquareMatrix[(inputSquareMatrix.GetLength(0) - 1 - i), layer];
                        //bottom to left
                        inputSquareMatrix[(inputSquareMatrix.GetLength(0) - 1 - i), layer] = inputSquareMatrix[inputSquareMatrix.GetLength(0) - 1 - layer, (inputSquareMatrix.GetLength(0) - 1 - i)];
                        //right to bottom
                        inputSquareMatrix[inputSquareMatrix.GetLength(0) - 1 - layer, (inputSquareMatrix.GetLength(0) - 1 - i)] = inputSquareMatrix[i, inputSquareMatrix.GetLength(0) - 1 - layer];
                        //top to right
                        inputSquareMatrix[i, inputSquareMatrix.GetLength(0) - 1 - layer] = temp;
                    }
                }
                PrintMatrix(inputSquareMatrix);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }
        public static void PrintMatrix(int[,] inputSquareMatrix)
        {
            for (int i = 0; i < inputSquareMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < inputSquareMatrix.GetLength(1); j++)
                {
                    Console.Write(inputSquareMatrix[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}

Output
======
1 2 3 5 6
1 2 3 5 6
1 2 3 5 6
1 2 3 5 6
1 2 3 5 6

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
5 5 5 5 5
6 6 6 6 6

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