Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace MatrixShuffing
  4. {
  5. class Program
  6. {
  7.  
  8.  
  9.  
  10. public static string[,] inputMatrix(int rows, int cows)
  11. {
  12. string[,] matrix = new string[rows, cows];
  13. for (int i = 0; i < rows; i++)
  14. {
  15. var row = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  16. for (int j = 0; j < cows; j++)
  17. {
  18. matrix[i, j] = row[j];
  19. }
  20. }
  21. return matrix;
  22. }
  23. public static void printIntMatrix(int rows, int cows, int[,] matrix)
  24. {
  25. for (int i = 0; i < rows; i++)
  26. {
  27. for (int j = 0; j < cows; j++)
  28. {
  29. Console.Write(matrix[i, j] + " ");
  30. }
  31. Console.WriteLine();
  32. }
  33. }
  34.  
  35. public static void swap(ref string x, ref string y)
  36. {
  37. string temp = x;
  38. x = y;
  39. y = temp;
  40. }
  41.  
  42. public static bool validateIndex(int index, int rows, int cows)
  43. {
  44. if (index < 0 || index >= rows || index >= cows)
  45. {
  46. return false;
  47. }
  48. return true;
  49. }
  50. static void Main(string[] args)
  51. {
  52. string input = Console.ReadLine();
  53. int[] tokens = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  54. int rows = tokens[0];
  55. int cows = tokens[1];
  56. string[,] matrix = inputMatrix(rows, cows);
  57. string comands = Console.ReadLine();
  58. while (comands != "END")
  59. {
  60. string[] arg = comands.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  61. string operateion = arg[0];
  62.  
  63. if (arg.Length != 5)
  64. {
  65. Console.WriteLine("Invalid input!");
  66. comands = Console.ReadLine();
  67. continue;
  68. }
  69.  
  70. int index1 = int.Parse(arg[1]);
  71. int index2 = int.Parse(arg[2]);
  72. int index3 = int.Parse(arg[3]);
  73. int index4 = int.Parse(arg[4]);
  74.  
  75. if ((index1 >= rows) || (index2 >= cows) || (index3 >= rows) || (index4 >= cows))
  76. {
  77. Console.WriteLine("Invalid input!");
  78. comands = Console.ReadLine();
  79. continue;
  80. }
  81. else
  82. {
  83. swap(ref matrix[index1, index2], ref matrix[index3, index4]);
  84. comands = Console.ReadLine();
  85. for (int i = 0; i < rows; i++)
  86. {
  87. for (int j = 0; j < cows; j++)
  88. {
  89. Console.Write(matrix[i, j] + " ");
  90. }
  91. Console.WriteLine();
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement