lmarkov

Numbers Arranged As Spiral

Dec 11th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. /*
  2.  * * Write a program that reads a positive integer number N (N < 20) from console and outputs in the console the numbers 1 ... N numbers arranged as a spiral.
  3.         Example for N = 4
  4.  *
  5. */
  6.  
  7. using System;
  8.  
  9. class NumbersArrangedAsSpiral
  10. {
  11.     static void Main()
  12.     {
  13.         byte numberN;        
  14.        
  15.         string invalidInput = "Please enter a valid number 0 < N < 20!" + Environment.NewLine;
  16.         Console.WriteLine("Enter a value for N:");
  17.         while (!(byte.TryParse(Console.ReadLine(), out numberN) && numberN > 0 && numberN < 20))
  18.         {
  19.             Console.WriteLine(invalidInput);
  20.             Console.WriteLine("Enter a value for N:");
  21.         }
  22.         Console.WriteLine();
  23.  
  24.         int[,] spiralMatrix = new int[numberN, numberN];
  25.         byte rows = 0;
  26.         byte columns = 0;
  27.         int elementsCount = 0;
  28.         byte currentRow = 0;
  29.         byte currentColumn = 0;
  30.         int maxRows = numberN - 1;        
  31.         int maxColumns = maxRows;
  32.  
  33.         while (!(elementsCount >= numberN * numberN))
  34.         {            
  35.             for (int i = currentColumn; i <= maxColumns; i++)
  36.             {
  37.                 elementsCount++;
  38.                 spiralMatrix[currentRow, i] = elementsCount;                                              
  39.             }
  40.             currentRow++;
  41.             for (int i = currentRow; i <= maxRows; i++)
  42.             {
  43.                 elementsCount++;
  44.                 spiralMatrix[i, maxColumns] = elementsCount;                            
  45.             }
  46.             maxColumns--;
  47.             for (int i = maxColumns; i >= currentColumn; i--)
  48.             {
  49.                 elementsCount++;
  50.                 spiralMatrix[maxRows, i] = elementsCount;                            
  51.             }
  52.             maxRows--;
  53.             for (int i = maxRows; i >= currentRow; i--)
  54.             {
  55.                 elementsCount++;
  56.                 spiralMatrix[i, currentColumn] = elementsCount;                                
  57.             }
  58.             currentColumn++;
  59.         }    
  60.  
  61.         for (rows = 0; rows < numberN; rows++)
  62.         {
  63.             for (columns = 0; columns < numberN; columns++)
  64.             {
  65.                 Console.Write("{0,5}", spiralMatrix[rows, columns]);
  66.             }
  67.             Console.WriteLine();
  68.         }                  
  69.         Console.WriteLine();
  70.         Main();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment