Advertisement
psabinski

C# Loops Ex.14

Nov 12th, 2012
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System;
  2.  
  3. class Sphirala
  4. {
  5.     static void Main()
  6.     {
  7.         int colStart, colEnd, oldColEnd;
  8.         int rowStart, rowEnd;
  9.         int rowFix,colFix;
  10.         int col, row;
  11.         int rowIndex, colIndex;
  12.         int num = 1;
  13.         int directionChange = 1;
  14.         int n = int.Parse(Console.ReadLine());
  15.      
  16.         int[,] matrix = new int[n,n];
  17.  
  18.  
  19.         for (int i = 0; i <= n - 1; i++)
  20.         {
  21.             matrix[0, i] = num;
  22.             num++;
  23.  
  24.         }
  25.  
  26.  
  27.         rowStart = rowIndex = 1;
  28.         rowEnd = n - 1;
  29.         colStart = colIndex = n - 2;
  30.         colEnd = 0;
  31.         rowFix = n - 1;
  32.         colFix = n - 1;
  33.  
  34.        
  35.         directionChange = 1;
  36.        
  37.         while (directionChange <= n-1)
  38.         {
  39.             if (directionChange % 2 != 0)                                    // down & left
  40.             {
  41.                 for (col = colFix, row = rowStart; row <= rowEnd; row++)
  42.                 {  
  43.  
  44.                     matrix[row,col] = num;
  45.                     num++;
  46.                 }
  47.  
  48.                 for (row = rowFix, col = colStart; col >= colEnd; col--)
  49.                 {
  50.                    
  51.                     matrix[row,col] = num;
  52.                     num++;
  53.                 }
  54.  
  55.                 rowEnd = rowStart;
  56.                 rowFix = rowStart;
  57.                 rowStart = colStart; // new rowStart
  58.  
  59.                 oldColEnd = colEnd;
  60.                 colEnd = colStart;
  61.                 colFix = oldColEnd; // old colEnd
  62.                 colStart = colFix + 1; // new colFix        
  63.  
  64.  
  65.             }
  66.  
  67.             else                                                    // up & right
  68.             {
  69.                 {
  70.                     for (col = colFix, row = rowStart; row >= rowEnd; row--)
  71.                     {
  72.                         matrix[row, col] = num;
  73.                         num++;
  74.                     }
  75.  
  76.                     rowFix = rowEnd;
  77.  
  78.  
  79.  
  80.                     for (row = rowFix, col = colStart; col <= colEnd; col++)
  81.                     {
  82.                         matrix[row, col] = num;
  83.                         num++;
  84.                     }
  85.  
  86.  
  87.                     colIndex--;
  88.                     rowIndex++;
  89.  
  90.                     colFix = colEnd; //old colEnd
  91.                     rowFix = rowStart;
  92.  
  93.                     colEnd = colStart; //old colStar
  94.                     colStart = colIndex;
  95.  
  96.                     rowEnd = rowStart;
  97.                     rowStart = rowIndex;
  98.  
  99.                 }
  100.             }
  101.             directionChange++;
  102.         }
  103.  
  104.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  105.         {
  106.             for (int cols = 0; cols < matrix.GetLength(1); cols++)
  107.             {
  108.                 string output = " "+ matrix[rows, cols];
  109.                 if (output.Length == 2)
  110.                 {
  111.                     output = output + " ";
  112.                 }
  113.  
  114.                 Console.Write(output);
  115.             }
  116.             Console.WriteLine();
  117.         }
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement