yanchevilian

Matrix Shuffling / C# Advanced

Aug 27th, 2021 (edited)
115
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.Generic;
  3. using System.Linq;
  4.  
  5. namespace TRAINING
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] dimensions = ReadFromTheConsole();
  12.  
  13.             int rows = int.Parse(dimensions[0]);
  14.             int cols = int.Parse(dimensions[1]);
  15.  
  16.            string[,] matrix =  FillTheMatrix(rows, cols);
  17.  
  18.            string command = Console.ReadLine();
  19.            while (command?.ToLower() != "end")
  20.            {
  21.                string[] cmdArr = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  22.                try
  23.                {
  24.                    string action = cmdArr[0];
  25.                    int rowIndexFromMatrix = int.Parse(cmdArr[1]);
  26.                    int colIndexFromMatrix = int.Parse(cmdArr[2]);
  27.                    int rowIndexOfItemToSwap = int.Parse(cmdArr[3]);
  28.                    int colIndexOfItemToSwap = int.Parse(cmdArr[4]);
  29.  
  30.                    string item1 = matrix[rowIndexFromMatrix, colIndexFromMatrix];
  31.                    string item2 = matrix[rowIndexOfItemToSwap, colIndexOfItemToSwap];
  32.                    matrix[rowIndexFromMatrix, colIndexFromMatrix] = item2;
  33.                    matrix[rowIndexOfItemToSwap, colIndexOfItemToSwap] = item1;
  34.                    PrintTheMatrix(matrix);
  35.                }
  36.                catch
  37.                {
  38.                    Console.WriteLine("Invalid input!");
  39.                }
  40.                 command = Console.ReadLine();
  41.             }
  42.         }
  43.  
  44.         private static void PrintTheMatrix(string[,] matrix)
  45.         {
  46.             for (int rowIndex = 0; rowIndex < matrix.GetLength(0); rowIndex++)
  47.             {
  48.                 for (int colIndex = 0; colIndex < matrix.GetLength(1); colIndex++)
  49.                 {
  50.                     Console.Write(matrix[rowIndex, colIndex] + " ");
  51.                 }
  52.  
  53.                 Console.WriteLine();
  54.             }
  55.         }
  56.  
  57.         public static string[,] FillTheMatrix(int rows, int cols)
  58.         {
  59.             string[,] matrix = new string[rows, cols];
  60.  
  61.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  62.             {
  63.                 string[] numbersToPut = ReadFromTheConsole();
  64.                 for (int colIndex = 0; colIndex < cols; colIndex++)
  65.                 {
  66.                     matrix[rowIndex, colIndex] = numbersToPut[colIndex];
  67.                 }
  68.             }
  69.             return matrix;
  70.         }
  71.  
  72.         public static string[] ReadFromTheConsole()
  73.         {
  74.             return Console.ReadLine()
  75.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  76.                 .ToArray();
  77.         }
  78.     }
  79. }
  80.  
Add Comment
Please, Sign In to add comment