Advertisement
Guest User

SpiralMatrix

a guest
Apr 3rd, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class SpiralMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("Enter the dimensions of the spiral: ");
  9.         int n = int.Parse(Console.ReadLine());
  10.         Console.Clear();
  11.        
  12.         int row = 0;
  13.         int col = -1;
  14.         string direction = "r";
  15.         int leftswitch = 0;
  16.         if (n > 0 && n <= 20)
  17.         {
  18.             int[,] matrix = new int[n, n];
  19.             for (int i = 1; i <= n * n; i++)
  20.             {
  21.                 if (direction == "r")
  22.                 {
  23.                     col++;
  24.                     matrix[row, col] = i;
  25.  
  26.                     if ((col == (n - 1) - row) && row <= n / 2)
  27.                     {
  28.                         direction = "d";
  29.  
  30.                     }
  31.                 }
  32.                 else if (direction == "d")
  33.                 {
  34.                     row++;
  35.                     matrix[row, col] = i;
  36.  
  37.                     if (row == col)
  38.                     {
  39.                         direction = "l";
  40.  
  41.                     }
  42.                 }
  43.                 else if (direction == "l")
  44.                 {
  45.                     col--;
  46.                     matrix[row, col] = i;
  47.  
  48.                     if (col == leftswitch)
  49.                     {
  50.                         leftswitch++;
  51.                         direction = "u";
  52.  
  53.                     }
  54.                 }
  55.                 else if (direction == "u")
  56.                 {
  57.                     row--;
  58.                     matrix[row, col] = i;
  59.  
  60.                     if (col == row - 1)
  61.                     {
  62.                         direction = "r";
  63.  
  64.                     }
  65.                 }
  66.             }
  67.             for (int p = 0; p < n; p++)
  68.             {
  69.                 for (int f = 0; f < n; f++)
  70.                 {
  71.                     if (n >= 10)
  72.                     {
  73.                         Console.Write(Convert.ToString((matrix[p, f])).PadRight(4, ' '));
  74.                     }
  75.                     else
  76.                     {
  77.                         Console.Write(Convert.ToString((matrix[p, f])).PadRight(3, ' '));
  78.                     }
  79.                 }
  80.                 Console.WriteLine();
  81.             }
  82.         }
  83.         else if(n<0 || n> 20)
  84.         {
  85.             Console.WriteLine("Invalid input! Valid inputs are (1 ≤ n ≤ 20) !");
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement