Print numbers in spiral order

Write a program that takes as input a positive integer N and prints out numbers 1 to N in spiral order. The number 1 is at center of the spiral and the numbers spiral outward counter-clockwise. E.g. if N = 100, the program should output:

spiral

Solution:

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length >= 1)
            {
                int N;
                if (int.TryParse(args[0], out N))
                {
                    if (N > 0)
                    {
                        var d = Math.Sqrt(N);
                        var m = (int)Math.Ceiling((d - 1) / 2);
                        var p = 2 * m + 1;
                        var a = new int[p, p];
                        fill(a, m, m, N);
                        print(a, N);
                    }
                }
            }
        }

        static void print (int[,] a, int N)
        {
            int L = N.ToString().Length;
            int p = a.GetLength(0);
            var fmt = "{0," + L + "} ";
            for(int row = 0; row < p; row++)
            {
                for(int col = 0; col < p; col++)
                {
                    if (a[row, col] == 0)
                    {
                        Console.Write(fmt, string.Empty);
                    }
                    else
                    {
                        Console.Write(fmt, a[row, col]);
                    }
                }
                Console.WriteLine();
            }
        }

        static void fill(int[,] a, int startRow, int startCol, int N)
        {
            int rowIncrement = 0;
            int colIncrement = 1;
            int row = startRow;
            int col = startCol;
            int ctr = 0;
            int L = 1;
            bool ff = false;
            for(int i = 1; i <= N; i++, row += rowIncrement, col += colIncrement, ctr++)
            {
                a[row, col] = i;
                if (ctr == L)
                {
                    changeDirection(ref rowIncrement, ref colIncrement);
                    ctr = 0;
                    if (ff)
                    {
                        L++;                        
                    }
                    ff = !ff;                    
                }                
            }
        }

        static void changeDirection(ref int r, ref int c)
        {
            if (r == 0 && c == 1)
            {
                r = -1;
                c = 0;
            }
            else if (r == -1 && c == 0)
            {
                r = 0;
                c = -1;
            }
            else if (r == 0 && c == -1)
            {
                r = 1;
                c = 0;
            }
            else if (r == 1 && c == 0)
            {
                r = 0;
                c = 1;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
    }
}

This entry was posted in Software. Bookmark the permalink.

Leave a comment