Pages

Monday, 29 January 2018

C# PROGRAM PRINT A MULTI-DIMENSIONAL ARRAY IN SNAIL SHELL WAY

C# PROGRAM PRINT A MULTI-DIMENSIONAL ARRAY IN SNAIL SHELL WAY
using System;


namespace SnailMatrix
{
   
    class Program
    {
        static void Main(string[] args)
        {
            //int rows, cols,
            int size;
            int a = 0;// b = 1;
            int i, j, k, count = 0, flag = 0;
           
          do
          {
             try
            {
                Console.WriteLine("\n>>>PROGRAM To PRINT A MULTI-DIMENSIONAL ARRAY walking IN A SNAIL SHELL WAY <<<");
                Console.Write("\n Enter the Size of a square Matrix(between 2 to 10):");
                size = Convert.ToInt16(Console.ReadLine());
                if (size<2||size > 10)   //limiting the size of matrix
                {
                    Console.BackgroundColor = ConsoleColor.DarkRed; //changing background color to red
                    Console.WriteLine("The Size Of Matrix should Be in between  2 and 10.");
                    //System.Console.ResetColor(); ///resetting color
                    Console.ReadKey(); return;
                }
              
                else
                {


                flag = 1;
                int[,] matrix = new int[size, size];

                for (k = 1; k < size; k++, a++)
                {
                    for (i = a, j = a; j < size - k; j++) //lef
                    {
                        matrix[i, j] = ++count;
                    }
                    for (i = a, j = size - k; i < size - k; i++) { matrix[i, j] = ++count; }
                    for (i = size - k, j = size - k; j >= a; j--)
                    {
                        matrix[i, j] = ++count;
                    }
                    for (i = size - (k + 1), j = a; i >= k; i--)
                    {
                        matrix[i, j] = ++count;
                    }
                }

              //  Console.Write("\n\n\t>>>  " + size + " * " + size + " MATRIX  <<<\n\t---------------------------------------\n\t");

                Console.Write("\n\n\t");
                for (i = 0; i < size; i++)
                {

                    for (j = 0; j < size; j++)
                    {
                        if (matrix[i, j] < 10) { Console.Write("  0" + Convert.ToString(matrix[i, j])); }
                        else
                        {
                            Console.Write("  " + Convert.ToString(matrix[i, j]));
                        }

                        if (j == size - 1) { Console.Write("\n\t"); }
                       
                    }
                }
            }
          }
            catch  //to catch exceptions,suppose string entered as a size of matrix
            {
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("WARNING:only Number between (2 and 10) is allowed");
                Console.ResetColor();
            }

             Console.WriteLine("\n\n\t Press any key to exit....");

          }while(flag==0);    
        
               Console.ReadKey();
    } 
   }

}