Advertisement
Guest User

Untitled

a guest
May 12th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4.  
  5. class MatrixShuffling
  6. {
  7.     static void Main()
  8.     {
  9.         int n = int.Parse(Console.ReadLine());
  10.         int m = int.Parse(Console.ReadLine());
  11.         string[,] matrix = new string[n, m];
  12.         for (int i = 0; i < n; i++)
  13.         {
  14.             for (int j = 0; j < m; j++)
  15.             {
  16.                 Console.WriteLine("Please enter element [{0},{1}]", i, j);
  17.                 matrix[i, j] = Console.ReadLine();
  18.             }
  19.         }
  20.         string command = Console.ReadLine();
  21.         while (command != "END")
  22.         {
  23.             string[] commandsSplit = command.Split();
  24.             int x1, x2, y1, y2;
  25.             try
  26.             {
  27.                 x1 = int.Parse(commandsSplit[1]);
  28.                 y1 = int.Parse(commandsSplit[2]);
  29.                 x2 = int.Parse(commandsSplit[3]);
  30.                 y2 = int.Parse(commandsSplit[4]);
  31.                 if (commandsSplit[0] != "swap" || !Enumerable.Range(0,n).Contains(x1) || !Enumerable.Range(0,n).Contains(x2)
  32.                     || !Enumerable.Range(0,m).Contains(y1) || !Enumerable.Range(0,m).Contains(y2))
  33.                 {
  34.                     throw new ArgumentOutOfRangeException();
  35.                 }
  36.             }
  37.             catch (Exception)
  38.             {
  39.                 Console.WriteLine("Invalid input!");
  40.                 command = Console.ReadLine();
  41.                 continue;
  42.             }
  43.             string temp = matrix[x1, y1];
  44.             matrix[x1, y1] = matrix[x2, y2];
  45.             matrix[x2, y2] = temp;
  46.             PrintMatrix(matrix, n, m);
  47.             command = Console.ReadLine();
  48.         }
  49.     }
  50.  
  51.     private static void PrintMatrix(string[,] matrix, int n, int m)
  52.     {
  53.         for (int i = 0; i < n; i++)
  54.         {
  55.             for (int j = 0; j < m; j++)
  56.             {
  57.                 Console.Write("{0} ", matrix[i, j]);
  58.             }
  59.             Console.WriteLine();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement