Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _4._Matrix_Shuffling
- {
- class Program
- {
- static void Main(string[] args)
- {
- var matrixSizes = GetMatrixSizes();
- int rows = int.Parse(matrixSizes[0]);
- int cols = int.Parse(matrixSizes[1]);
- string[,] matrix = ReadMatrix(rows, cols);
- string command = Console.ReadLine();
- while (command?.ToLower() != "end")
- {
- string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- if (tokens.Length == 5 && tokens[0] == "swap" && int.Parse(tokens[1]) < rows && int.Parse(tokens[2]) < cols)
- {
- int firstRow = int.Parse(tokens[1]);
- int firstCol = int.Parse(tokens[2]);
- int secondRow = int.Parse(tokens[3]);
- int secondCol = int.Parse(tokens[4]);
- string temp = matrix[firstRow, firstCol];
- matrix[firstRow, firstCol] = matrix[secondRow, secondCol];
- matrix[secondRow, secondCol] = temp;
- PrintMatrix(matrix);
- }
- else
- {
- Console.WriteLine("Invalid input!");
- command = Console.ReadLine();
- continue;
- }
- command = Console.ReadLine();
- }
- }
- private static void PrintMatrix(string[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col] + " ");
- }
- Console.WriteLine();
- }
- }
- static string[] GetMatrixSizes()
- {
- string[] matrixSizes = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries);
- return matrixSizes;
- }
- static string[,] ReadMatrix(int rows, int cols)
- {
- string[,] matrix = new string[rows, cols];
- for (int row = 0; row < rows; row++)
- {
- string[] inputArr = GetMatrixSizes();
- for (int col = 0; col < cols; col++)
- {
- matrix[row, col] = inputArr[col];
- }
- }
- return matrix;
- }
- }
- }
Add Comment
Please, Sign In to add comment