Advertisement
dimipan80

6.12Loops_NumbersInSpiralMatrix

Mar 25th, 2014
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2.  
  3. class SquareN_NumbersInSpiralMatrix
  4. {
  5.     static void Main ()
  6.     {
  7.         Console.Write("Please, enter a positive whole number up to 20, for N = ");
  8.         string numberStr = Console.ReadLine();
  9.         uint numN = uint.Parse(numberStr);        
  10.         if (numN < 1 || numN > 20)
  11.         {
  12.             Console.WriteLine("Error - The Number is Out of Range !!!");
  13.         }
  14.         else
  15.         {
  16.             int [,] spiralMatrix = new int [numN, numN];
  17.             int row = 0;
  18.             int col = 0;
  19.             string direction = "right";
  20.             uint maxValue = numN * numN;
  21.  
  22.             for (int i = 1; i <= maxValue; i++)
  23.             {
  24.                 if (direction == "right" && (col > (numN - 1) || spiralMatrix [row, col] != 0))
  25.                 {
  26.                     direction = "down";
  27.                     row++;
  28.                     col--;                    
  29.                 }
  30.                 if (direction == "down" && (row > (numN - 1) || spiralMatrix [row, col] != 0))
  31.                 {
  32.                     direction = "left";
  33.                     row--;
  34.                     col--;
  35.                 }
  36.                 if (direction == "left" && (col < 0 || spiralMatrix [row, col] != 0))
  37.                 {
  38.                     direction = "up";
  39.                     row--;
  40.                     col++;
  41.                 }
  42.                 if (direction == "up" && (row < 0 || spiralMatrix [row, col] != 0))
  43.                 {
  44.                     direction = "right";
  45.                     row++;
  46.                     col++;
  47.                 }
  48.  
  49.                 spiralMatrix [row, col] = i;
  50.                 if (direction == "right")
  51.                 {
  52.                     col++;
  53.                 }
  54.                 if (direction == "down")
  55.                 {
  56.                     row++;
  57.                 }
  58.                 if (direction == "left")
  59.                 {
  60.                     col--;
  61.                 }
  62.                 if (direction == "up")
  63.                 {
  64.                     row--;
  65.                 }
  66.             }
  67.            
  68.             // Print Spiral Matrix:
  69.             Console.WriteLine("The Spiral Matrix from 1 to square of N numbers is:");
  70.             for (int r = 0; r < numN; r++)
  71.             {
  72.                 for (int c = 0; c < numN; c++)
  73.                 {
  74.                     Console.Write("{0,4}", spiralMatrix [r, c]);
  75.                 }
  76.                 Console.WriteLine();          
  77.             }            
  78.         }
  79.         Console.ReadLine();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement