Advertisement
ElviraPetkova

JaggedArray2

Oct 2nd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _06._Jagged_Array_Manipulator
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int numberOfRows = int.Parse(Console.ReadLine());
  11.  
  12.             double[][] jaggedArray = new double[numberOfRows][];
  13.  
  14.             for (int row = 0; row < numberOfRows; row++)
  15.             {
  16.                 jaggedArray[row] = Console.ReadLine()
  17.                     .Split(" ")
  18.                     .Select(double.Parse)
  19.                     .ToArray();
  20.             }
  21.  
  22.             for (int row = 0; row < numberOfRows - 1; row++)
  23.             {
  24.                 if (jaggedArray[row].Length == jaggedArray[row + 1].Length)
  25.                 {
  26.                     jaggedArray[row] = jaggedArray[row].Select(x => x * 2).ToArray();
  27.                     jaggedArray[row + 1] = jaggedArray[row + 1].Select(x => x * 2).ToArray();
  28.                 }
  29.                 else
  30.                 {
  31.                     jaggedArray[row] = jaggedArray[row].Select(x => x / 2).ToArray();
  32.                     jaggedArray[row + 1] = jaggedArray[row + 1].Select(x => x / 2).ToArray();
  33.                 }
  34.             }
  35.  
  36.             string input;
  37.  
  38.             while((input = Console.ReadLine()) != "End")
  39.             {
  40.                 string[] commands = input.Split(" ");
  41.                 string command = commands[0];
  42.                 int row = int.Parse(commands[1]);
  43.                 int col = int.Parse(commands[2]);
  44.                 double value = double.Parse(commands[3]);
  45.  
  46.                 bool isValid = ValidationRangeMatrix(jaggedArray, row, col);
  47.  
  48.                 if (isValid)
  49.                 {
  50.                     switch (command)
  51.                     {
  52.                         case "Add":
  53.                             jaggedArray[row][col] += value;
  54.                             break;
  55.                         case "Subtract":
  56.                             jaggedArray[row][col] -= value;
  57.                             break;
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             for (int row = 0; row < numberOfRows; row++)
  63.             {
  64.                 Console.WriteLine(string.Join(" ", jaggedArray[row]));
  65.             }
  66.         }
  67.  
  68.         private static bool ValidationRangeMatrix(double[][] jaggedArray, int row, int col)
  69.             => row >= 0 && jaggedArray.GetLength(0) > row && col >= 0 && jaggedArray[row].Length > col ?
  70.             true : false;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement