Advertisement
Guest User

1.1.7.12. FourTypesOfSquareMatrice

a guest
Jan 26th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.82 KB | None | 0 0
  1. // 1.1.7.12. Write a program, which creates square matrices like those in the figures below and prints them
  2. // formatted to the console. The size of the matrices will be read from the console.
  3. // See the examples for matrices with size of 4 x 4 and make the other sizes in a similar fashion:
  4. //     1 5  9 13     1 8  9 16     7 11 14 16     1 12 11 10
  5. //  a) 2 6 10 14  b) 2 7 10 15  c) 4  8 12 15  d) 2 13 16  9
  6. //     3 7 11 15     3 6 11 14     2  5  9 13     3 14 15  8
  7. //     4 8 12 16     4 5 12 13     1  3  6 10     4  5  6  7
  8.  
  9. using System;
  10.  
  11. class FourTypesOfSquareMatrice
  12. {
  13.     private static char CorrectTypeInput()
  14.     {
  15.         char type = '\u0000';
  16.         bool correctType = false;
  17.         do
  18.         {
  19.             Console.Write("Pick a type ('a', 'b', 'c' or 'd'): ");
  20.             string userInput = Console.ReadLine();
  21.             correctType = char.TryParse(userInput, out type);
  22.         } while (correctType == false);
  23.         return type;
  24.     }
  25.     private static uint CorrectSizeInput()
  26.     {
  27.         uint size = 0u;
  28.         bool correctSize = false;
  29.         do
  30.         {
  31.             Console.Write("Choose a size (size > 0): ");
  32.             string userInput = Console.ReadLine();
  33.             correctSize = uint.TryParse(userInput, out size);
  34.         } while (correctSize == false);
  35.         return size;
  36.     }
  37.     private static void ConstructMatriceA(uint size, int[,] matrice)
  38.     {
  39.         for (int col = 0; col < size; col++)
  40.         {
  41.             for (int row = 0; row < size; row++)
  42.             {
  43.                 int stepper = ((col * (int)size) + 1) + row;
  44.                 matrice[row, col] = stepper;
  45.             }
  46.         }
  47.     }
  48.     private static void ConstructMatriceB(uint size, int[,] matrice)
  49.     {
  50.         for (int col = 0; col < size; col++)
  51.         {
  52.             if (col % 2 == 0)
  53.             {
  54.                 for (int row = 0; row < size; row++)
  55.                 {
  56.                     int stepper = ((col * (int)size) + 1) + row;
  57.                     matrice[row, col] = stepper;
  58.                 }
  59.             }
  60.             else
  61.             {
  62.                 for (int row = (int)size - 1, counter = 0; row >= 0; row--)
  63.                 {
  64.                     int stepper = ((col * (int)size) + 1) + counter;
  65.                     matrice[row, col] = stepper;
  66.                     counter++;
  67.                 }
  68.             }            
  69.         }
  70.     }
  71.     private static void ConstructMatriceC(uint size, int[,] matrice)
  72.     {
  73.         int startPositionX = 0;
  74.         int startPositionY = (int)size - 1;
  75.         int stepper = 1;
  76.         do
  77.         {
  78.             int currentPositionX = startPositionX;
  79.             int currentPositionY = startPositionY;
  80.             while (currentPositionX < size && currentPositionY < size)
  81.             {
  82.                 matrice[currentPositionY, currentPositionX] = stepper;
  83.                 stepper++;
  84.                 currentPositionX++;
  85.                 currentPositionY++;
  86.             }
  87.             if (startPositionY != 0)
  88.             {
  89.                 startPositionY--;
  90.             }
  91.             else
  92.             {
  93.                 startPositionX++;
  94.             }
  95.         } while (stepper <= size * size);
  96.     }
  97.     private static void ConstructMatriceD(uint size, int[,] matrice)
  98.     {
  99.         int stepper = 1;
  100.         int row = 0;
  101.         int col = 0;
  102.         int endRightCol = (int)size - 1;
  103.         int endDownRow = (int)size - 1;
  104.         int endLeftCol = 1;
  105.         int endUpRow = 0;
  106.         while (true)
  107.         {
  108.             for (row = row; row <= endDownRow; row++)   // down
  109.             {
  110.                 matrice[row, col] = stepper;
  111.                 stepper++;
  112.             }
  113.             row--;
  114.             endDownRow--;
  115.             col++;
  116.             for (col = col; col <= endRightCol; col++)  // right
  117.             {
  118.                 matrice[row, col] = stepper;
  119.                 stepper++;
  120.             }
  121.             col--;
  122.             endRightCol--;
  123.             row--;
  124.             for (row = row; row >= endUpRow; row--) // up
  125.             {
  126.                 matrice[row, col] = stepper;
  127.                 stepper++;
  128.             }
  129.             if (stepper > size * size)
  130.             {
  131.                 break;
  132.             }
  133.             row++;
  134.             endUpRow++;
  135.             col--;
  136.             for (col = col; col >= endLeftCol; col--)   // left
  137.             {
  138.                 matrice[row, col] = stepper;
  139.                 stepper++;
  140.             }
  141.             col++;
  142.             endLeftCol++;
  143.             row++;
  144.         }
  145.     }
  146.     private static void PrintMatrice(uint size, int[,] matrice)
  147.     {
  148.         for (int row = 0; row < size; row++)
  149.         {
  150.             for (int col = 0; col < size; col++)
  151.             {
  152.                 Console.Write("{0, -3}", matrice[row, col]);
  153.             }
  154.             Console.WriteLine();
  155.         }
  156.     }
  157.     static void Main()
  158.     {
  159.         while (true)
  160.         {
  161.             char type = CorrectTypeInput();
  162.             uint size = CorrectSizeInput();
  163.             int[,] matrice = new int[size, size];
  164.  
  165.             if (type == 'a')
  166.             {
  167.                 ConstructMatriceA(size, matrice);
  168.                 PrintMatrice(size, matrice);
  169.             }
  170.             else if (type == 'b')
  171.             {
  172.                 ConstructMatriceB(size, matrice);
  173.                 PrintMatrice(size, matrice);
  174.             }
  175.             else if (type == 'c')
  176.             {
  177.                 ConstructMatriceC(size, matrice);
  178.                 PrintMatrice(size, matrice);
  179.             }
  180.             else if (type == 'd')
  181.             {
  182.                 ConstructMatriceD(size, matrice);
  183.                 PrintMatrice(size, matrice);
  184.             }
  185.             else
  186.             {
  187.                 Console.WriteLine("There is no such a type of matrice");
  188.             }
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement