Printing a 2D matrix in diagonally spiral order

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Collections;

 

namespace ConsoleApplication1

{

    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 diagonally spiral form: ");

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

            int counter = 0;

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

            {

                //Diagonally Down

                for (; j >= 0 && i < rows; i++,j–)

                {

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

                    counter++;

                    if (counter == rows * cols)

                    {

                        Console.WriteLine();

                        break;

                    }

                    else

                    {

                        Console.Write(", ");

                    }

                }

                if (j == -1 && i < rows)

                {

                    j++;

                }

                else

                {

                    i–; j = j + 2;

                }

                //Diagonally Up

                for (; i >= 0 && j < cols; i–, j++)

                {

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

                    counter++;

                    if (counter == rows * cols)

                    {

                        Console.WriteLine();

                        break;

                    }

                    else

                    {

                        Console.Write(", ");

                    }

                }

                if (i < 0 && j < cols)

                {

                    i++;

                }

                else

                {

                    j–; i = i + 2;

                }

            }

        }

    }

}

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