stefan1919

FillTheMatrix

Sep 22nd, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 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 ConsoleApplication7
  8. {
  9.     class Program
  10.     {
  11.        
  12.         static void Main(string[] args)
  13.         {
  14.             int size = int.Parse(Console.ReadLine());
  15.             int[,] matrix = new int[size, size];
  16.             int rows = matrix.GetLength(1);
  17.             int cols = matrix.GetLength(0);
  18.             int counterOutput = 1;
  19.             for (int i = 0; i < rows; i++)
  20.             {
  21.                 if (i % 2 == 0)
  22.                 {
  23.                     for (int j = 0; j < cols; j++)
  24.                 {
  25.                     matrix[j , i] = counterOutput;
  26.                     counterOutput++;
  27.                 }
  28.                 }
  29.                 else
  30.                 {
  31.                     for (int j = cols - 1; j >= 0; j--)
  32.                     {
  33.                         matrix[j, i] = counterOutput;
  34.                         counterOutput++;
  35.                     }  
  36.                 }
  37.             }
  38.  
  39.             Console.WriteLine("The matrix is as follows:");
  40.             for (int row = 0; row < matrix.GetLength(0); row++)
  41.             {
  42.                 for (int col = 0; col < matrix.GetLength(1); col++)
  43.                 {
  44.                     Console.Write("{0} ", matrix[row, col]);
  45.                 }
  46.  
  47.                 Console.WriteLine();
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment