Advertisement
Guest User

Untitled

a guest
May 8th, 2015
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FillMatrix_1
  8. {
  9.     class FillMatrix_1
  10.     {
  11.         static int n = 0;
  12.  
  13.         static void MatrixPatternA()
  14.         {
  15.             int[,] matrixA = new int[n, n];
  16.             int counter = 1;
  17.             for (int col = 0; col < n; col++)
  18.             {
  19.                 for (int row = 0; row < n; row++)
  20.                 {
  21.                     matrixA[row, col] = counter;
  22.                     counter++;
  23.                 }
  24.             }
  25.             PrintMatrix(matrixA);
  26.         }
  27.         static void MatrixPatternB()
  28.         {
  29.             int[,] matrixB = new int[n, n];
  30.             int counter = 1;
  31.             for (int col = 0; col < n; col++)
  32.             {
  33.                 if (col % 2 == 0)
  34.                 {
  35.                     for (int row = 0; row < n; row++)
  36.                     {
  37.                         matrixB[row, col] = counter;
  38.                         counter++;
  39.                     }
  40.                 }
  41.                 else
  42.                 {
  43.                     for (int row = n - 1; row >= 0; row--)
  44.                     {
  45.                         matrixB[row, col] = counter;
  46.                         counter++;
  47.                     }
  48.                 }
  49.             }          
  50.             PrintMatrix(matrixB);
  51.         }
  52.         static void PrintMatrix(int[,] matrix)
  53.         {
  54.             for (int a = 0; a < matrix.GetLength(0); a++)
  55.             {
  56.                 for (int b = 0; b < matrix.GetLength(1); b++)
  57.                 {
  58.                     Console.Write("|{0,2}|",matrix[a, b]);
  59.                 }
  60.                 Console.WriteLine();
  61.             }
  62.         }
  63.         static void Main(string[] args)
  64.         {
  65.             n = int.Parse(Console.ReadLine());
  66.             MatrixPatternA();
  67.             Console.WriteLine();
  68.             MatrixPatternB();
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement