Advertisement
stak441

MultidimensionalArrays - problem 1

Jan 15th, 2013
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _01.PrintMatrixNN
  7. {
  8.     class PrintMatrixNN
  9.     {
  10.         static void PrintMatrix(int size, int[,] matrix)      //Method for printing the matrix
  11.         {
  12.             for (int i = 0; i < size; i++)            
  13.             {
  14.                 for (int j = 0; j < size; j++)
  15.                 {
  16.                     Console.Write("{0,-4}", matrix[i, j]);
  17.                 }
  18.                 Console.WriteLine();
  19.             }
  20.             Console.WriteLine();
  21.         }
  22.  
  23.         static void GenerateMatrixA(int n, int[,] matrix)
  24.         {
  25.             for (int column = 0, counter = 1; column < n; column++)
  26.             {
  27.                 for (int row = 0; row < n; row++)
  28.                 {
  29.                     matrix[row, column] = counter++;
  30.                 }
  31.             }
  32.  
  33.             PrintMatrix(n, matrix);
  34.         }
  35.  
  36.         static void GenerateMatrixB(int n, int[,] matrix)
  37.         {
  38.             for (int column = 0, counter = 1, direction = 1; column < n; column++, direction++)
  39.             {
  40.                 if (direction % 2 != 0)             //Direction DOWN, when odd number
  41.                 {
  42.                     for (int row = 0; row < n; row++)
  43.                     {
  44.                         matrix[row, column] = counter++;
  45.                     }
  46.                 }
  47.                 else                                //Direction UP, when even number
  48.                 {
  49.                     for (int row = n - 1; row >= 0; row--)
  50.                     {
  51.                         matrix[row, column] = counter++;
  52.                     }
  53.                 }
  54.  
  55.             }
  56.  
  57.             PrintMatrix(n, matrix);
  58.         }
  59.  
  60.  
  61.         static void GenerateMatrixC(int n, int[,] matrix)
  62.         {
  63.             int counter = 1;
  64.             for (int row = n - 1; row >= 0; row--)                 //Works from bottom-left to diagonal
  65.             {
  66.                 for (int col = 0; col < n - row; col++)
  67.                 {
  68.                     matrix[row + col, col] = counter++;
  69.                 }
  70.  
  71.             }
  72.  
  73.             for (int col = 1; col < n; col++)                     //Mirror operation
  74.             {
  75.                 for (int row = 0; row < n - col; row++)
  76.                 {
  77.                     matrix[row, row + col] = counter++;
  78.                 }
  79.             }
  80.  
  81.             PrintMatrix(n, matrix);
  82.                      
  83.  
  84.         }
  85.  
  86.  
  87.         static void GenerateMatrixD(int n, int[,] matrix)
  88.         {
  89.             int startRow = 0;
  90.             int startCol = 0;
  91.             int maxRow = n - 1;
  92.             int maxCol = n - 1;
  93.             int counter = 1;
  94.  
  95.             while (counter <= n * n)
  96.             {
  97.                 //direction Down:
  98.                 for (int row = startRow; row <= maxRow; row++)
  99.                 {
  100.                     matrix[row, startCol] = counter++;
  101.                 }
  102.                 startCol++;
  103.  
  104.                 //direction Rigth:
  105.                 for (int column = startCol; column <= maxCol; column++)
  106.                 {
  107.                     matrix[maxRow, column] = counter++;
  108.                 }
  109.                 maxRow--;
  110.  
  111.                 //direction Up:
  112.                 for (int row = maxRow; row >= startRow; row--)
  113.                 {
  114.                     matrix[row, maxCol] = counter++;
  115.                 }
  116.                 maxCol--;
  117.  
  118.                 //direction Left:
  119.                 for (int column = maxCol; column >= startCol; column--)
  120.                 {
  121.                     matrix[startRow, column] = counter++;
  122.                 }
  123.                 startRow++;
  124.             }
  125.  
  126.  
  127.             PrintMatrix(n, matrix);
  128.         }
  129.  
  130.  
  131.         static void Main(string[] args)
  132.         {
  133.             int n = 10;
  134.             int[,] matrix = new int[n, n];
  135.  
  136.             GenerateMatrixA(n, matrix);
  137.             GenerateMatrixB(n, matrix);
  138.             GenerateMatrixC(n, matrix);
  139.             GenerateMatrixD(n, matrix);
  140.  
  141.            
  142.         }
  143.        
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement