borko9696

Problem 1. Fill the Matrix

Sep 20th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 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. class FillТheMatrix
  8. {
  9.     static void Main()
  10.     {
  11.         //matrix Patern A
  12.         int n = int.Parse(Console.ReadLine());
  13.        
  14.         int count=1;
  15.        
  16.         int[,] matrix = new int[n, n];
  17.        
  18.  
  19.         for (int row = 0; row < n; row++)
  20.         {
  21.             for (int col = 0; col < n; col++)
  22.             {
  23.                 matrix[row, col] = count;
  24.                  count+= n;
  25.             }
  26.             count = (row + 2);
  27.         }
  28.  
  29.         for (int row = 0; row < n; row++)
  30.         {
  31.             for (int col = 0; col < n; col++)
  32.             {
  33.                 Console.Write("{0} ",matrix[row,col]);
  34.             }
  35.             Console.WriteLine();
  36.         }
  37.         Console.WriteLine();
  38.  
  39.         //matrix Patern B
  40.         int[,] matrix1 = new int[n, n];
  41.         int count1=1;
  42.  
  43.         for (int col = 0; col < n; col++)
  44.         {
  45.             for (int rowSnake=n-1, row = 0; row < n;rowSnake--, row++)
  46.             {
  47.                 if (col%2==0)
  48.                 {
  49.                     matrix1[row, col] = count1;
  50.                     count1++;
  51.                 }
  52.  
  53.                 if (col % 2 != 0)
  54.                 {
  55.  
  56.                     matrix1[rowSnake, col] = count1;
  57.                     count1++;
  58.                 }
  59.             }
  60.  
  61.         }
  62.  
  63.         for (int row = 0; row < n; row++)
  64.         {
  65.             for (int col = 0; col < n; col++)
  66.             {
  67.                 Console.Write("{0} ", matrix1[row, col]);
  68.             }
  69.             Console.WriteLine();
  70.         }
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment