Advertisement
Guest User

Multidimensional Arrays tricks

a guest
Jul 20th, 2013
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. //Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4)
  2.  
  3. using System;
  4.  
  5. namespace FillPrintMatrix
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11.     Console.Write("Please enter the matix size: ");
  12.     string input = Console.ReadLine();
  13.     int N;
  14.     int digit = 1;
  15.  
  16.     while (int.TryParse(input, out N) == false)
  17.     {
  18.         Console.Write("Please enter a valid integer!: ");
  19.         input = Console.ReadLine();
  20.     }
  21.     int[,] Matrix = new int[N, N];
  22.     Console.WriteLine();
  23.  
  24.     //// Variant A:
  25.     for (int col = 0; col < N; col++)
  26.     {
  27.         for (int row = 0; row < N; row++)
  28.         {
  29.             Matrix[row, col] = digit;       //Fill in the matrix
  30.             digit++;
  31.         }
  32.     }
  33.  
  34.     Console.WriteLine("This is variant A:");
  35.     for (int row = 0; row < N; row++)
  36.     {
  37.         for (int col = 0; col < N; col++)
  38.         {
  39.             Console.Write("{0,4}", Matrix[row, col]);     //Print the matrix
  40.         }
  41.         Console.WriteLine();
  42.     }
  43.     digit = 1;
  44.     Console.WriteLine();
  45.  
  46.     // Variant B:
  47.     for (int col = 0; col < N; col++)
  48.     {
  49.         if (col % 2 == 0)
  50.         {
  51.             for (int row = 0; row < N; row++)
  52.             {
  53.                 Matrix[row, col] = digit;       //Fill in the matrix
  54.                 digit++;
  55.             }
  56.         }
  57.         else
  58.         {
  59.             for (int row = N - 1; row >= 0; row--)
  60.             {
  61.                 Matrix[row, col] = digit;       //Fill in the matrix
  62.                 digit++;
  63.             }
  64.         }
  65.     }
  66.  
  67.     Console.WriteLine("This is variant B:");
  68.     for (int row = 0; row < N; row++)
  69.     {
  70.         for (int col = 0; col < N; col++)
  71.         {
  72.             Console.Write("{0,4}", Matrix[row, col]);     //Print the matrix
  73.         }
  74.         Console.WriteLine();
  75.     }
  76.     digit = 1;
  77.     Console.WriteLine();
  78.  
  79.     // Variant C:
  80.     int r = N - 1;
  81.     int c = 0;
  82.     int StartRow = N - 1;
  83.     int StartCol = 0;
  84.     Matrix[r, c] = 1;
  85.  
  86.     while (digit < N * N)
  87.     {
  88.         //First logic
  89.         //Start position
  90.         if (r == (N - 1) && c < (N - 1))
  91.         {
  92.             StartRow--;
  93.             StartCol = 0;
  94.             r = StartRow;       // Row = (N-1), (N-2), (N-N)
  95.             c = StartCol;       // Col = 0
  96.             digit++;
  97.             Matrix[r, c] = digit;
  98.  
  99.             //down-right movement -> row++; col++; until bottom-right is reached
  100.             while (r < (N - 1) && c < (N - 1))
  101.             {
  102.                 r++;    //Move down
  103.                 c++;    //Move right
  104.                 digit++;
  105.                 Matrix[r, c] = digit;
  106.             }
  107.         }
  108.         //Start position, when bottom-right corner reached
  109.         if (r <= (N - 1) && c == (N - 1))
  110.         {
  111.             StartRow = 0;
  112.             StartCol++;
  113.             r = StartRow;   // Row = 0
  114.             c = StartCol;   // Col = 1,2,3,4...N-1
  115.             digit++;        // digit = 1,2,3...N*N
  116.             Matrix[r, c] = digit;
  117.  
  118.             //down-right movement -> row++; col++; until rightest column is reached. The bottom is not N-1 anymore!
  119.             while (c < (N - 1))
  120.             {
  121.                 r++;    //Move down
  122.                 c++;    //Move right
  123.                 digit++;
  124.                 Matrix[r, c] = digit;
  125.             }
  126.         }
  127.     }
  128.  
  129.     Console.WriteLine("This is variant C:");
  130.     for (int row = 0; row < N; row++)
  131.     {
  132.         for (int col = 0; col < N; col++)
  133.         {
  134.             Console.Write("{0,4}", Matrix[row, col]);     //Print the matrix
  135.         }
  136.         Console.WriteLine();
  137.     }
  138.     digit = 1;
  139.     Console.WriteLine();
  140.  
  141.     //Variant D:
  142.     int offset = 0;
  143.     int Row = 0;
  144.     int Col = 0;
  145.  
  146.     while (digit <= N*N)
  147.     {
  148.         //Move down
  149.         for (Row = offset; Row < N-offset; Row++)
  150.         {
  151.             Col = offset;
  152.             Matrix[Row, Col] = digit;
  153.             digit++;
  154.         }
  155.         //Move right
  156.         for (Col = 1+offset; Col < N-offset; Col++)
  157.         {
  158.             Row = N - 1 - offset;
  159.             Matrix[Row, Col] = digit;
  160.             digit++;
  161.         }
  162.         //Move up
  163.         for (Row = N-2-offset; Row >= offset; Row--)
  164.         {
  165.             Col = N - 1 - offset;
  166.             Matrix[Row, Col] = digit;
  167.             digit++;
  168.         }
  169.         //Move left
  170.         for (Col = N-2-offset; Col >= offset+1; Col--)
  171.         {
  172.             Row = offset;
  173.             Matrix[Row, Col] = digit;
  174.             digit++;
  175.         }
  176.         offset++;
  177.     }
  178.  
  179.     Console.WriteLine("This is variant D:");
  180.     for (int row = 0; row < N; row++)
  181.     {
  182.         for (int col = 0; col < N; col++)
  183.         {
  184.             Console.Write("{0,4}", Matrix[row, col]);     //Print the matrix
  185.         }
  186.         Console.WriteLine();
  187.     }
  188.     digit = 1;
  189.     Console.WriteLine();
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement