Advertisement
Guest User

Untitled

a guest
Dec 30th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using System;
  2. //Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4)
  3.  
  4. class FillArrays
  5. {
  6.     static int n;
  7.     static int[,] myArray = new int[n, n];
  8.  
  9.     static void Main(string[] args)
  10.     {
  11.         Console.WriteLine("Please enter the dimentions of the array: ");
  12.         n = int.Parse(Console.ReadLine());
  13.         Console.WriteLine("First result:");
  14.         FirstSolution(myArray);
  15.         Console.WriteLine();
  16.         Console.WriteLine("Second result:");
  17.         SecondSolution(myArray);
  18.         Console.WriteLine();
  19.         Console.WriteLine("Third result:");
  20.         ThirdSolution(myArray);
  21.         Console.WriteLine();
  22.         Console.WriteLine("Fourth result:");
  23.         FourthSolution(myArray);
  24.  
  25.     }
  26.  
  27.     static void PrintMatrix(int[,] arr)
  28.     {
  29.         for (int row = 0; row < arr.GetLength(0); row++)
  30.         {
  31.             for (int col = 0; col < arr.GetLength(1); col++)
  32.             {
  33.                 Console.Write("{0, -3}", arr[row, col]);
  34.             }
  35.  
  36.             Console.WriteLine();
  37.         }
  38.     }
  39.     //solution a)
  40.     static void FirstSolution(int[,] array)
  41.     {
  42.         array = new int[n, n];
  43.         int number = 1;
  44.  
  45.         for (int row = 0; row < array.GetLength(1); row++)
  46.         {
  47.             for (int col = 0; col < array.GetLength(0); col++)
  48.             {
  49.                 array[row, col] = number;
  50.                 number = (number + n);
  51.             }
  52.             number -= n * n;
  53.             number++;
  54.  
  55.         }
  56.         PrintMatrix(array);
  57.     }
  58.     //solution b)
  59.     static void SecondSolution(int[,] array)
  60.     {
  61.         array = new int[n, n];
  62.         int number = 1;
  63.         for (int col = 0; col < array.GetLength(1); col += 2)
  64.         {
  65.             for (int row = 0; row < array.GetLength(0); row++)
  66.             {
  67.                 array[row, col] = number;
  68.                 number++;
  69.             }
  70.             number += n;
  71.         }
  72.         if (n % 2 != 0)
  73.         {
  74.             number -= (n * n);
  75.         }
  76.         else number -= (n * n) - n;
  77.  
  78.         for (int col = 1; col < array.GetLength(1); col += 2)
  79.         {
  80.             for (int row = array.GetLength(1) - 1; row >= 0; row--)
  81.             {
  82.                 array[row, col] = number;
  83.                 number++;
  84.             }
  85.             number += n;
  86.         }
  87.         PrintMatrix(array);
  88.     }
  89.     //solution c)
  90.     static void ThirdSolution(int[,] array)
  91.     {
  92.         array = new int[n, n];
  93.         int startRow = n - 1;
  94.         int startCol = 0;
  95.         int row = n - 1;
  96.         int col = 0;
  97.         int value = 1;
  98.         array[row, col] = value; //1
  99.  
  100.         while (value < n * n)
  101.         {
  102.             if (row == n - 1 && col < n - 1)
  103.             {
  104.                 startRow--;
  105.                 startCol = 0;
  106.                 row = startRow;
  107.                 col = startCol;       // Col = 0
  108.                 value++;
  109.                 array[row, col] = value;
  110.  
  111.                 while (row < (n - 1) && col < (n - 1))
  112.                 {
  113.                     row++;    //Move down
  114.                     col++;    //Move right
  115.                     value++;
  116.                     array[row, col] = value;
  117.                 }
  118.             }
  119.  
  120.             if (row <= (n - 1) && col == (n - 1))
  121.             {
  122.                 startRow = 0;
  123.                 startCol++;
  124.                 row = startRow;   // Row = 0
  125.                 col = startCol;   // Col = 1,2,3,4...N-1
  126.                 value++;
  127.                 array[row, col] = value;
  128.  
  129.                 while (col < (n - 1))
  130.                 {
  131.                     row++;    //Move down
  132.                     col++;    //Move right
  133.                     value++;
  134.                     array[row, col] = value;
  135.                 }
  136.             }
  137.         }
  138.         PrintMatrix(array);
  139.     }
  140.  
  141.     //solution d)
  142.     static void FourthSolution(int[,] array)
  143.     {
  144.         array = new int[n, n];
  145.         string direction = "down";
  146.         int value = 1;
  147.         int row = 0;
  148.         int col = 0;
  149.         while (value <= n * n)
  150.         {
  151.             array[row, col] = value++;
  152.             if (direction == "down" && (row == n - 1 || array[row + 1, col] != 0)) { direction = "right"; }
  153.             if (direction == "right" && (col == n - 1 || array[row, col + 1] != 0)) { direction = "up"; }
  154.             if (direction == "up" && (row == 0 || array[row - 1, col] != 0)) { direction = "left"; }
  155.             if (direction == "left" && (col == 0 || array[row, col - 1] != 0)) { direction = "down"; }
  156.  
  157.             if (direction == "down")
  158.             {
  159.                 row++;
  160.             }
  161.             if (direction == "right")
  162.             {
  163.                 col++;
  164.             }
  165.             if (direction == "up")
  166.             {
  167.                 row--;
  168.             }
  169.             if (direction == "left")
  170.             {
  171.                 col--;
  172.             }
  173.         }
  174.  
  175.         PrintMatrix(array);
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement