Advertisement
Guest User

[C#] Домашно Multidimensional Arrays - 1 Задача

a guest
Dec 19th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. //Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4).
  2.  
  3. using System;
  4. class FillsAndPrintsAMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         int n = int.Parse(Console.ReadLine());
  9.  
  10.         int[,] matrix;
  11.  
  12.         matrix=MatrixA(n);
  13.         PrintMatrix(matrix);
  14.         matrix = MatrixB(n);
  15.         PrintMatrix(matrix);
  16.         matrix = MatrixC(n);
  17.         PrintMatrix(matrix);
  18.     }
  19.     static int[,]MatrixA(int n)
  20.     {
  21.         int[,] matrix = new int[n, n];
  22.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  23.         {
  24.             int counter = rows;
  25.             counter++;
  26.             for (int col = 0; col < matrix.GetLength(1); col++, counter += n)
  27.             {
  28.                 matrix[rows, col] = counter;
  29.             }
  30.         }
  31.         return matrix;
  32.     }
  33.     static int[,]MatrixB(int n)
  34.     {
  35.         int[,] matrix = new int[n, n];
  36.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  37.         {
  38.             int counter = rows;
  39.             int counter2 = 0;
  40.             counter++;
  41.             for (int col = 0; col < matrix.GetLength(1); col++, counter += n)
  42.             {
  43.                 if (col % 2 == 0)
  44.                 {
  45.                     matrix[rows, col] = counter;
  46.                 }
  47.                 else
  48.                 {
  49.                     counter2 = n * (col + 1);
  50.                     counter2 -= rows;
  51.                     matrix[rows, col] = counter2;
  52.                 }
  53.             }
  54.         }
  55.         return matrix;
  56.     }
  57.     static int[,]MatrixC(int n)
  58.     {
  59.         int[,] matrix = new int[n, n];
  60.         int counter = 1;
  61.         for (int rows = matrix.GetLength(0) - 1; rows >= 0; rows--)
  62.         {
  63.             for (int col = 0; col < n - rows; col++)
  64.             {
  65.                 matrix[(rows + col), col] = counter;
  66.                 counter++;
  67.             }
  68.         }
  69.         for (int col = 1; col < matrix.GetLength(1); col++)
  70.         {
  71.             for (int rows = 0; rows < n - col; rows++)
  72.             {
  73.                 matrix[rows, (rows + col)] = counter;
  74.                 counter++;
  75.             }
  76.         }
  77.         return matrix;
  78.     }
  79.     static void PrintMatrix(int [,] matrix)
  80.     {
  81.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  82.         {
  83.             for (int col = 0; col < matrix.GetLength(1); col++)
  84.             {
  85.                 Console.Write("{0,4}",matrix[rows,col]);
  86.             }
  87.             Console.WriteLine();
  88.         }
  89.         Console.WriteLine();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement