Advertisement
Guest User

Untitled

a guest
May 8th, 2015
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 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 MatrixShuffling_3
  8. {
  9.     class MatrixShuffling_3
  10.     {
  11.         static string[,] matrix;
  12.  
  13.         static void SwapElements(int x1,int y1,int x2,int y2)
  14.         {
  15.             string temp = matrix[x1, y1];
  16.             matrix[x1, y1] = matrix[x2, y2];
  17.             matrix[x2, y2] = temp;
  18.  
  19.             PrintMatrix();
  20.         }
  21.         static void PrintMatrix()
  22.         {
  23.             for (int i = 0; i < matrix.GetLength(0); i++)
  24.             {
  25.                 for (int j = 0; j < matrix.GetLength(1); j++)
  26.                 {
  27.                     Console.Write("|{0,2}|",matrix[i,j]);
  28.                 }
  29.                 Console.WriteLine();
  30.             }
  31.         }
  32.         static void Main(string[] args)
  33.         {
  34.             int row = int.Parse(Console.ReadLine());
  35.             int col = int.Parse(Console.ReadLine());
  36.             matrix = new string [row, col];
  37.             bool isEnd = false;
  38.  
  39.             for (int a = 0; a < row; a++)
  40.             {
  41.                 for (int b = 0; b < col; b++)
  42.                 {
  43.                     Console.Write("Enter [{0}, {1}] element: ", a, b);
  44.                     matrix[a, b] = Console.ReadLine();
  45.                 }
  46.             }
  47.             while (!isEnd)
  48.             {
  49.                 string swap = Console.ReadLine();
  50.                 if (swap == "END")
  51.                 {
  52.                     isEnd = true;
  53.                     break;
  54.                 }
  55.                 string[] input = swap.Split().ToArray();
  56.                 if (input[0] != "swap" && input.Length != 5)
  57.                 {
  58.                     Console.WriteLine("Invalid input!");
  59.                 }
  60.                 else
  61.                 {
  62.                     int x1 = int.Parse(input[1]);
  63.                     int y1 = int.Parse(input[2]);
  64.                     int x2 = int.Parse(input[3]);
  65.                     int y2 = int.Parse(input[4]);
  66.                     if ((x1 > row || x2 > row) || (y1 > col || y2 > col))
  67.                     {
  68.                         Console.WriteLine("Invalid input!");
  69.                     }
  70.                     else
  71.                     {
  72.                         SwapElements(x1, y1, x2, y2);
  73.                     }                  
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement