Venciity

[ Telerik C#] MultidimArrays 01-MatrixInitializationC

Feb 26th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void PrintMatrix(int[,] matrix)
  6.     {
  7.         int cellSize = (int)Math.Log10(matrix.Length) + 1;
  8.         for (int i = 0; i < matrix.GetLength(0); i++)
  9.             for (int j = 0; j < matrix.GetLength(1); j++)
  10.                 Console.Write(Convert.ToString(matrix[i, j]).PadRight(cellSize, ' ') + (j != matrix.GetLength(1) - 1 ? " " : "\n"));
  11.     }
  12.  
  13.     static void Main()
  14.     {
  15.         int n = 4;
  16.  
  17.         int[,] matrix = new int[n, n];
  18.         int counter = 1;
  19.  
  20.         for (int i = 0; i <= n - 1; i++)
  21.             for (int j = 0; j <= i; j++) matrix[n - i + j - 1, j] = counter++;
  22.  
  23.         for (int i = n - 2; i >= 0; i--)
  24.             for (int j = i; j >= 0; j--) matrix[i - j, n - j - 1] = counter++;
  25.  
  26.         PrintMatrix(matrix);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment