Advertisement
Venciity

[ Telerik C#] MultidimArrays 01-MatrixInitializationB

Feb 26th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 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.         for (int i = 0, counter = 1; i < n; i++)
  19.             if (i % 2 == 0) for (int j = 0; j < n; j++) matrix[j, i] = counter++;
  20.             else for (int j = n - 1; j >= 0; j--) matrix[j, i] = counter++;
  21.  
  22.         PrintMatrix(matrix);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement