Advertisement
dimipan80

6.14Loops_SpiralMatrix_StartFrom1Colon

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