yanchevilian

04. Matrix Shuffling from C# Advanced

Jul 19th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _4._Matrix_Shuffling
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var matrixSizes = GetMatrixSizes();
  11.  
  12.             int rows = int.Parse(matrixSizes[0]);
  13.             int cols = int.Parse(matrixSizes[1]);
  14.  
  15.             string[,] matrix = ReadMatrix(rows, cols);
  16.  
  17.             string command = Console.ReadLine();
  18.  
  19.             while (command?.ToLower() != "end")
  20.             {
  21.                 string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  22.                 if (tokens.Length == 5 && tokens[0] == "swap" && int.Parse(tokens[1]) < rows && int.Parse(tokens[2]) < cols)
  23.                 {
  24.                     int firstRow = int.Parse(tokens[1]);
  25.                     int firstCol = int.Parse(tokens[2]);
  26.                     int secondRow = int.Parse(tokens[3]);
  27.                     int secondCol = int.Parse(tokens[4]);
  28.  
  29.                     string temp = matrix[firstRow, firstCol];
  30.                     matrix[firstRow, firstCol] = matrix[secondRow, secondCol];
  31.                     matrix[secondRow, secondCol] = temp;
  32.                     PrintMatrix(matrix);
  33.                 }
  34.                 else
  35.                 {
  36.                     Console.WriteLine("Invalid input!");
  37.                     command = Console.ReadLine();
  38.                     continue;
  39.                 }
  40.  
  41.                 command = Console.ReadLine();
  42.             }
  43.         }
  44.  
  45.         private static void PrintMatrix(string[,] matrix)
  46.         {
  47.             for (int row = 0; row < matrix.GetLength(0); row++)
  48.             {
  49.                 for (int col = 0; col < matrix.GetLength(1); col++)
  50.                 {
  51.                     Console.Write(matrix[row, col] + " ");
  52.                 }
  53.  
  54.                 Console.WriteLine();
  55.             }
  56.         }
  57.  
  58.         static string[] GetMatrixSizes()
  59.         {
  60.             string[] matrixSizes = Console.ReadLine()
  61.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  62.  
  63.  
  64.             return matrixSizes;
  65.         }
  66.  
  67.         static string[,] ReadMatrix(int rows, int cols)
  68.         {
  69.             string[,] matrix = new string[rows, cols];
  70.  
  71.             for (int row = 0; row < rows; row++)
  72.             {
  73.                 string[] inputArr = GetMatrixSizes();
  74.  
  75.                 for (int col = 0; col < cols; col++)
  76.                 {
  77.                     matrix[row, col] = inputArr[col];
  78.                 }
  79.             }
  80.  
  81.             return matrix;
  82.  
  83.         }
  84.     }
  85. }
  86.  
Add Comment
Please, Sign In to add comment