Advertisement
gabi11

Multidimensional Arrays - 04. Matrix shuffling

May 12th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var dimentions = Console.ReadLine()
  14.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(int.Parse)
  16.                 .ToArray();
  17.  
  18.             int rows = dimentions[0];
  19.             int cols = dimentions[1];
  20.  
  21.             var matrix = new string[rows, cols];
  22.  
  23.             for (int row = 0; row < rows; row++)
  24.             {
  25.                 var currentRow = Console.ReadLine()
  26.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  27.                 .ToArray();
  28.  
  29.                 for (int col = 0; col < cols; col++)
  30.                 {
  31.                     matrix[row, col] = currentRow[col];
  32.                 }
  33.             }
  34.  
  35.             while (true)
  36.             {
  37.                 var input = Console.ReadLine();
  38.  
  39.                 if (input == "END")
  40.                 {
  41.                     break;
  42.                 }
  43.  
  44.                 var line = input.Split().ToArray();
  45.  
  46.                 var command = line[0];
  47.  
  48.                 if (command != "swap" || line.Length != 5)
  49.                 {
  50.                     Console.WriteLine("Invalid input!");
  51.                 }
  52.  
  53.                 else
  54.                 {
  55.                     var row1 = int.Parse(line[1]);
  56.                     var col1 = int.Parse(line[2]);
  57.                     var row2 = int.Parse(line[3]);
  58.                     var col2 = int.Parse(line[4]);
  59.  
  60.                     if (row1 < 0 || row1 >= rows
  61.                         || col1 < 0 || col1 >= cols
  62.                         || row2 < 0 || row2 >= rows
  63.                         || col2 < 0 || col2 >= cols)
  64.                     {
  65.                         Console.WriteLine("Invalid input!");
  66.                     }
  67.  
  68.                     else
  69.                     {
  70.                         var firstIndex = matrix[row1, col1];
  71.                         var secondIndex = matrix[row2, col2];
  72.  
  73.                         matrix[row1, col1] = secondIndex;
  74.                         matrix[row2, col2] = firstIndex;
  75.  
  76.                         for (int row = 0; row < rows; row++)
  77.                         {
  78.                             for (int col = 0; col < cols; col++)
  79.                             {
  80.                                 Console.Write(matrix[row, col] + " ");
  81.                             }
  82.                             Console.WriteLine();
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement