Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace TRAINING
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] dimensions = ReadFromTheConsole();
- int rows = int.Parse(dimensions[0]);
- int cols = int.Parse(dimensions[1]);
- string[,] matrix = FillTheMatrix(rows, cols);
- string command = Console.ReadLine();
- while (command?.ToLower() != "end")
- {
- string[] cmdArr = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- try
- {
- string action = cmdArr[0];
- int rowIndexFromMatrix = int.Parse(cmdArr[1]);
- int colIndexFromMatrix = int.Parse(cmdArr[2]);
- int rowIndexOfItemToSwap = int.Parse(cmdArr[3]);
- int colIndexOfItemToSwap = int.Parse(cmdArr[4]);
- string item1 = matrix[rowIndexFromMatrix, colIndexFromMatrix];
- string item2 = matrix[rowIndexOfItemToSwap, colIndexOfItemToSwap];
- matrix[rowIndexFromMatrix, colIndexFromMatrix] = item2;
- matrix[rowIndexOfItemToSwap, colIndexOfItemToSwap] = item1;
- PrintTheMatrix(matrix);
- }
- catch
- {
- Console.WriteLine("Invalid input!");
- }
- command = Console.ReadLine();
- }
- }
- private static void PrintTheMatrix(string[,] matrix)
- {
- for (int rowIndex = 0; rowIndex < matrix.GetLength(0); rowIndex++)
- {
- for (int colIndex = 0; colIndex < matrix.GetLength(1); colIndex++)
- {
- Console.Write(matrix[rowIndex, colIndex] + " ");
- }
- Console.WriteLine();
- }
- }
- public static string[,] FillTheMatrix(int rows, int cols)
- {
- string[,] matrix = new string[rows, cols];
- for (int rowIndex = 0; rowIndex < rows; rowIndex++)
- {
- string[] numbersToPut = ReadFromTheConsole();
- for (int colIndex = 0; colIndex < cols; colIndex++)
- {
- matrix[rowIndex, colIndex] = numbersToPut[colIndex];
- }
- }
- return matrix;
- }
- public static string[] ReadFromTheConsole()
- {
- return Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- }
- }
- }
Add Comment
Please, Sign In to add comment