Advertisement
stefan1919

MatrixShuffling

Sep 22nd, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication7
  8. {
  9.     class Program
  10.     {
  11.        
  12.         static void Main(string[] args)
  13.         {
  14.             int rows = int.Parse(Console.ReadLine());
  15.             int cols = int.Parse(Console.ReadLine());
  16.             int[,] matrix = new int[rows, cols];
  17.             string input = Console.ReadLine();
  18.             List<int> measures = new List<int>();
  19.             List<List<string>> swapper = new List<List<string>>();
  20.             while (input != "END")
  21.             {
  22.                 if (input.Length == 1)
  23.                 {
  24.                     measures.Add(int.Parse(input));
  25.                 }
  26.                 if (input.Length > 1)
  27.                 {
  28.                     string[] inputArray = input.Split(' ');
  29.                    
  30.                         List<string> toSwap = new List<string>();
  31.                         for (int i = 0; i < inputArray.Length; i++)
  32.                         {
  33.                             toSwap.Add(inputArray[i]);
  34.                         }
  35.                         swapper.Add(toSwap);
  36.                 }
  37.                 input = Console.ReadLine();
  38.             }
  39.             int counter = 0;
  40.             for (int i = 0; i < matrix.GetLength(0); i++)
  41.             {
  42.                 for (int j = 0; j < matrix.GetLength(1); j++)
  43.                 {
  44.                     matrix[i, j] = measures[counter];
  45.                     counter++;
  46.                 }
  47.             }
  48.             /*for (int k = 0; k < matrix.GetLength(0); k++)
  49.             {
  50.                 for (int p = 0; p < matrix.GetLength(1); p++)
  51.                 {
  52.                     Console.Write(matrix[k, p] + " ");
  53.                 }
  54.                 Console.WriteLine();
  55.             }*/
  56.             int exceptionWriteRows = matrix.GetLength(0);
  57.             int exceptionWriteCols = matrix.GetLength(1);
  58.             foreach (var list in swapper)
  59.             {
  60.                 if (list[0] == "swap")
  61.                 {
  62.  
  63.                     string help = string.Empty;
  64.             //        for (int i = 0; i < list.Count; i++)
  65.             //{
  66.             //    help.Join(list[i]);
  67.             //}
  68.                     string[] helpTransformArray = help.Split(' ');
  69.                     int swappedRow = 0;
  70.                     int swappedCol = 0;
  71.                     int swapperRow = 0;
  72.                     int swapperCol = 0;
  73.                     swappedRow = int.Parse(list[1]);
  74.                     swappedCol = int.Parse(list[2]);
  75.                     swapperRow = int.Parse(list[3]);
  76.                     swapperCol = int.Parse(list[4]);
  77.                     if ((swappedRow < matrix.GetLength(0)) && (swappedCol < matrix.GetLength(1)) && (swapperRow < matrix.GetLength(0)  && (swapperCol < matrix.GetLength(1))))
  78.                     {
  79.                         int hanger = matrix[swappedRow, swappedCol];
  80.                         matrix[swappedRow, swappedCol] = matrix[swapperRow, swapperCol];
  81.                         matrix[swapperRow, swapperCol] = hanger;
  82.                         Console.WriteLine("(after swapping {0} and {1}):", hanger, matrix[swappedRow, swappedCol]);
  83.                         printMatrix(matrix);
  84.                     }
  85.  
  86.                     else
  87.                     {
  88.                         Console.WriteLine("Invalid input!");
  89.                         Console.WriteLine();
  90.                     }
  91.                 }
  92.                 else
  93.                 {
  94.                     Console.WriteLine("Invalid input!");
  95.                     Console.WriteLine();
  96.                 }
  97.             }
  98.         }
  99.         static void printMatrix(int[,] matrix)
  100.         {
  101.             for (int k = 0; k < matrix.GetLength(0); k++)
  102.             {
  103.                 for (int p = 0; p < matrix.GetLength(1); p++)
  104.                 {
  105.                     Console.Write(matrix[k, p] + " ");
  106.                 }
  107.                 Console.WriteLine();
  108.             }
  109.             Console.WriteLine();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement