Printing a 2D matrix in spiral order

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            int[,] matrix = {

                                { 1, 2, 3, 4 },

                                { 5, 6, 7, 8 },

                                { 9, 10, 11, 12 },

                                { 13, 14, 15, 16 }

                             };//given matrix

            int rows = matrix.GetLength(0);

            int cols = matrix.GetLength(1);

            Console.WriteLine("Given matrix: ");

            Console.WriteLine("==============");

            for (int i = 0; i < rows; i++)

            {

                for (int j = 0; j < cols; j++)

                {

                    Console.Write(matrix[i, j] + " ");

                }

                Console.WriteLine();

            }

            Console.WriteLine("Printing the matrix in spiral form: ");

            Console.WriteLine("====================================");

            int counter = 0;

            for (int i = 0, j = 0, k = 0; counter < rows * cols; k++)

            {

                //Right

                for (; j < cols – k; j++)

                {

                    Console.Write(matrix[i, j]);

                    counter++;

                    if (counter == rows * cols)

                    {

                        Console.WriteLine();

                        break;

                    }

                    else

                    {

                        Console.Write(", ");

                    }

                }

                j–; i++;

                //Down

                for (; i < rows – k; i++)

                {

                    Console.Write(matrix[i, j]);

                    counter++;

                    if (counter == rows * cols)

                    {

                        Console.WriteLine();

                        break;

                    }

                    else

                    {

                        Console.Write(", ");

                    }

                }

                i–; j–;

                //Left

                for (; j >= k; j–)

                {

                    Console.Write(matrix[i, j]);

                    counter++;

                    if (counter == rows * cols)

                    {

                        Console.WriteLine();

                        break;

                    }

                    else

                    {

                        Console.Write(", ");

                    }

                }

                j++; i–;

                //Up

                for (; i >= 1 + k; i–)

                {

                    Console.Write(matrix[i, j]);

                    counter++;

                    if (counter == rows * cols)

                    {

                        Console.WriteLine();

                        break;

                    }

                    else

                    {

                        Console.Write(", ");

                    }

                }

                i++; j++;

            }

        }

    }

}

 

Given matrix:
==============
 1   2   3   4
 5   6   7   8
 9  10 11 12
13 14 15 16
Printing the matrix in spiral form:
====================================
1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10
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