Advertisement
Guest User

Matrix Rotation

a guest
Apr 13th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2.  
  3. class Week2MatrixRotation
  4. {
  5.     static void Main()
  6.     {
  7.         int n = 3;
  8.         int[,] matrix = new int[n, n];
  9.         /* The counter will be filling the matrices, as the numbers in them are to be consecutive */
  10.         int counter;
  11.  
  12.         // filling the matrix
  13.         counter = 1;
  14.         for (int row = 0; row < n; row++)
  15.         {
  16.             for (int col = 0; col < n; col++)
  17.             {
  18.                 matrix[col, row] = counter;
  19.                 counter++;
  20.             }
  21.         }
  22.  
  23.         // 1st print
  24.         for (int rows = 0; rows < n; rows++)
  25.         {
  26.             for (int cols = 0; cols < n; cols++)
  27.             {
  28.                 // changed places of rows and columns, so that we can get the columns printed one by one
  29.                 Console.Write("{0,3}", matrix[cols, rows]);
  30.             }
  31.             Console.WriteLine();
  32.         }
  33.         Console.WriteLine();
  34.  
  35.         // 2rd print: 1st 90% rotation
  36.         for (int rows = 0; rows < n; rows++)
  37.         {
  38.             for (int cols = n - 1; cols >= 0; cols--)
  39.             {
  40.                 Console.Write("{0,3}", matrix[rows, cols]);
  41.             }
  42.             Console.WriteLine();
  43.         }
  44.         Console.WriteLine();
  45.  
  46.         // 3rd print: 2nd 90% rotation
  47.         for (int rows = n - 1; rows >= 0; rows--)
  48.         {
  49.             for (int cols = n - 1; cols >= 0; cols--)
  50.             {
  51.                 Console.Write("{0,3}", matrix[cols, rows]);
  52.             }
  53.             Console.WriteLine();
  54.         }
  55.         Console.WriteLine();
  56.  
  57.         // 4th print: 3rd 90% rotation
  58.         for (int rows = n - 1; rows >= 0; rows--)
  59.         {
  60.             for (int cols = 0; cols < n; cols++)
  61.             {
  62.                 Console.Write("{0,3}", matrix[rows, cols]);
  63.             }
  64.             Console.WriteLine();
  65.         }
  66.         Console.WriteLine();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement