PetarNeshkov5360

Jagged array manipulator

Feb 27th, 2021
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. int rows = int.Parse(Console.ReadLine());
  2. double[][] matrix = new double[rows][];
  3. string input = "";
  4. for (int row = 0; row < matrix.GetLength(0); row++)
  5. {
  6. double[] cols = Console.ReadLine().Split().Select(double.Parse).ToArray();
  7. matrix[row] = new double[cols.Length];
  8. for (int col = 0; col < cols.Length; col++)
  9. {
  10. matrix[row][col] = cols[col];
  11. }
  12. }
  13. for (int row = 0; row < matrix.GetLength(0)-1; row++)
  14. {
  15. if (matrix[row].Length == matrix[row + 1].Length)
  16. {
  17. for (int rowCurrent = row; rowCurrent <= row + 1; rowCurrent++)
  18. {
  19. for (int col = 0; col < matrix[rowCurrent].Length; col++)
  20. {
  21. matrix[rowCurrent][col] *=2;
  22. }
  23. }
  24. }
  25. else
  26. {
  27. for (int rowCurrent = row; rowCurrent <= row + 1; rowCurrent++)
  28. {
  29. for (int col = 0; col < matrix[rowCurrent].Length; col++)
  30. {
  31. matrix[rowCurrent][col] /=2;
  32. }
  33. }
  34. }
  35. }
  36. while ((input=Console.ReadLine())!="End")
  37. {
  38. string[] information = input.Split();
  39. if (information[0]=="Add")
  40. {
  41. int row = int.Parse(information[1]);
  42. int col = int.Parse(information[2]);
  43. int value = int.Parse(information[3]);
  44. if (row>=0&&row<=rows&&col>=0&&col<=matrix[row].Length)
  45. {
  46. matrix[row][col] += value;
  47. }
  48. }
  49. else if (information[0]=="Subtract")
  50. {
  51. int row = int.Parse(information[1]);
  52. int col = int.Parse(information[2]);
  53. int value = int.Parse(information[3]);
  54. if (row >= 0 && row <= rows && col >= 0 && col <= matrix[row].Length)
  55. {
  56. matrix[row][col] -= value;
  57. }
  58. }
  59. }
  60. for (int row = 0; row < matrix.GetLength(0); row++)
  61. {
  62. for (int col = 0; col < matrix[row].Length; col++)
  63. {
  64. Console.Write($"{matrix[row][col]} ");
  65. }
  66. Console.WriteLine();
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment