Advertisement
DennyGD

FillTheMatrix

Feb 8th, 2015
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.11 KB | None | 0 0
  1. //Write a program that fills and prints a matrix of size (n, n) as shown below:
  2.  
  3. //Example for n=4:
  4. //  a)                   b)                       c)                  d)*
  5. //  1   5   9   13       1  8   9   16            7 11  14  16        1  12  11  10
  6. //  2   6   10  14       2  7   10  15            4  8  12  15        2  13  16   9
  7. //  3   7   11  15       3  6   11  14            2  5  9   13        3  14  15   8
  8. //  4   8   12  16       4  5   12  13            1  3  6   10        4   5   6   7
  9.  
  10. using System;
  11.  
  12. namespace FillTheMatrix
  13. {
  14.     class FillTheMatrix
  15.     {
  16.         static void Main()
  17.         {
  18.             Console.WriteLine("Please enter n.");
  19.             int n = Int32.Parse(Console.ReadLine());
  20.  
  21.             int[,] filledMatrix = FillMatrixA(n);
  22.             Console.WriteLine("Option A:");
  23.             Console.WriteLine();
  24.             PrintMatrix(filledMatrix);
  25.             Console.WriteLine("Option B:");
  26.             Console.WriteLine();
  27.             filledMatrix = FillMatrixB(n);
  28.             PrintMatrix(filledMatrix);
  29.             Console.WriteLine("Option D:");
  30.             Console.WriteLine();
  31.             filledMatrix = FillMatrixD(n);
  32.             PrintMatrix(filledMatrix);
  33.  
  34.         }
  35.  
  36.         static int[,] FillMatrixA(int n)
  37.         {
  38.             int[,] matrixA = new int[n, n];
  39.             int counter = 1;
  40.  
  41.             for (int col = 0; col < n; col++)
  42.             {
  43.                 for (int row = 0; row < n; row++)
  44.                 {
  45.                     matrixA[col, row] = counter;
  46.                     counter++;
  47.                 }
  48.             }
  49.             return matrixA;
  50.         }
  51.  
  52.         static int[,] FillMatrixB(int n)
  53.         {
  54.             int[,] matrixB = new int[n, n];
  55.            
  56.             int counterEven = 0;
  57.             int multiplyNBy = 1;
  58.             int counterOdd = n * 2;
  59.  
  60.             for (int col = 0; col < n; col++)
  61.             {
  62.                 if (col % 2 != 0)
  63.                 {
  64.                     counterEven = counterOdd;
  65.                 }
  66.  
  67.                 for (int row = 0; row < n; row++)
  68.                 {
  69.                     if ((col == 0) || (col % 2 == 0))
  70.                     {
  71.                         counterEven++;
  72.                         matrixB[col, row] = counterEven;
  73.                     }
  74.                     else
  75.                     {
  76.                         matrixB[col, row] = counterOdd;
  77.                         counterOdd--;
  78.                     }
  79.                 }
  80.                 multiplyNBy++;
  81.                 counterOdd = n * multiplyNBy;
  82.             }
  83.  
  84.             return matrixB;
  85.         }
  86.  
  87.         static int[,] FillMatrixD(int n)
  88.         {
  89.             int[,] matrixD = new int[n, n];
  90.             int[,] beenThereDoneThat = new int[n, n];
  91.             string direction = "down";
  92.             int directionCounter = 1;
  93.             int counter = 2;
  94.             int row = 0;
  95.             int col = 0;
  96.             matrixD[row, col] = 1;
  97.             row++;
  98.  
  99.             while (counter <= n * n)
  100.             {
  101.                 switch (directionCounter)
  102.                 {
  103.                     case 1: direction = "down";
  104.                         break;
  105.                     case 2: direction = "right";
  106.                         break;
  107.                     case 3: direction = "up";
  108.                         break;
  109.                     case 4: direction = "left";
  110.                         break;
  111.                     default: directionCounter = 0;
  112.                         break;
  113.                 }
  114.                 if (direction == "down")
  115.                 {
  116.                     if (matrixD[row, col] == 0)
  117.                     {
  118.                         matrixD[row, col] = counter;
  119.                         counter++;
  120.                     }
  121.                    
  122.                     if (row < n -1)
  123.                     {
  124.                         row++;
  125.                     }
  126.                     else
  127.                     {
  128.                         directionCounter = 2;
  129.                         col++;
  130.                     }
  131.                 }
  132.                 else if (direction == "right")
  133.                 {
  134.                     if (matrixD[row, col] == 0)
  135.                     {
  136.                         matrixD[row, col] = counter;
  137.                         counter++;
  138.                     }
  139.                    
  140.                     if (col < n -1)
  141.                     {
  142.                         col++;
  143.                     }
  144.                     else
  145.                     {
  146.                         directionCounter = 3;
  147.                         row = col;
  148.                     }
  149.                 }
  150.                 else if (direction == "up")
  151.                 {
  152.                     if (matrixD[row, col] == 0)
  153.                     {
  154.                         matrixD[row, col] = counter;
  155.                         counter++;
  156.                     }
  157.                    
  158.                     if (row > 0)
  159.                     {
  160.                         row--;
  161.                     }
  162.                     else
  163.                     {
  164.                         directionCounter = 4;
  165.                     }
  166.                 }
  167.                 else if (direction == "left")
  168.                 {
  169.                     if (matrixD[row, col] == 0)
  170.                     {
  171.                         matrixD[row, col] = counter;
  172.                         counter++;
  173.                     }
  174.                    
  175.                     if (col > 0)
  176.                     {
  177.                         col--;
  178.                     }
  179.                     else
  180.                     {
  181.                         directionCounter = 1;
  182.                     }
  183.                 }
  184.             }
  185.             return matrixD;
  186.         }
  187.  
  188.         static void PrintMatrix(int[,] filledMatrix)
  189.         {
  190.             for (int row = 0; row < filledMatrix.GetLength(1); row++)
  191.             {
  192.                 for (int col = 0; col < filledMatrix.GetLength(0); col++)
  193.                 {
  194.                     Console.Write(filledMatrix[col, row] + " ");
  195.                 }
  196.                 Console.Write(Environment.NewLine + Environment.NewLine);
  197.             }
  198.  
  199.             Console.WriteLine();
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement