Advertisement
Guest User

Strange matrix

a guest
Jul 19th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FillPrintMatrix
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9.     Console.Write("Please enter the matix size: ");
  10.     string input = Console.ReadLine();
  11.     int N;
  12.     int digit = 1;
  13.  
  14.     while (int.TryParse(input, out N) == false)
  15.     {
  16.         Console.Write("Please enter a valid integer!: ");
  17.         input = Console.ReadLine();
  18.     }
  19.     int[,] Matrix = new int[N, N];
  20.     Console.WriteLine();
  21.  
  22.     //// Variant A:
  23.     for (int col = 0; col < N; col++)
  24.     {
  25.         for (int row = 0; row < N; row++)
  26.         {
  27.             Matrix[row, col] = digit;       //Fill in the matrix
  28.             digit++;
  29.         }
  30.     }
  31.  
  32.     Console.WriteLine("This is variant A:");
  33.     for (int row = 0; row < N; row++)
  34.     {
  35.         for (int col = 0; col < N; col++)
  36.         {
  37.             Console.Write("{0,4}", Matrix[row, col]);     //Print the matrix
  38.         }
  39.         Console.WriteLine();
  40.     }
  41.     digit = 1;
  42.     Console.WriteLine();
  43.  
  44.     // Variant B:
  45.     for (int col = 0; col < N; col++)
  46.     {
  47.         if(col % 2 == 0)
  48.         {
  49.             for (int row = 0; row < N; row++)
  50.             {
  51.                 Matrix[row, col] = digit;       //Fill in the matrix
  52.                 digit++;
  53.             }
  54.         }
  55.         else
  56.         {
  57.             for (int row = N-1; row >= 0; row--)
  58.             {
  59.                 Matrix[row, col] = digit;       //Fill in the matrix
  60.                 digit++;
  61.             }
  62.         }
  63.     }
  64.  
  65.     Console.WriteLine("This is variant B:");
  66.     for (int row = 0; row < N; row++)
  67.     {
  68.         for (int col = 0; col < N; col++)
  69.         {
  70.             Console.Write("{0,4}", Matrix[row, col]);     //Print the matrix
  71.         }
  72.         Console.WriteLine();
  73.     }
  74.     digit = 1;
  75.     Console.WriteLine();
  76.  
  77.     }
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement