yanchevilian

JaggedArray Manipulator / C# Advanced

Aug 29th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Security.Cryptography.X509Certificates;
  6.  
  7. namespace _6._Jagged_Array_Manipulator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int rows = int.Parse(Console.ReadLine());
  14.             double[][] jaggedMatrix = new double[rows][];
  15.  
  16.             FillTheJaggedMatrix(rows, jaggedMatrix);
  17.             CheckForEqualsRows(jaggedMatrix);
  18.            
  19.             //    •   "Add {row} {column} {value}" - add { value}
  20.             //to the element at the given indexes, if they are valid
  21.             //    •   "Subtract {row} {column} {value}" - subtract { value}
  22.             //from the element at the given indexes, if they are valid
  23.             //    //• "End" - print the final state of the matrix(all elements separated by a single space) and stop the program
  24.  
  25.             AddOrSubtract(jaggedMatrix);
  26.  
  27.             Console.WriteLine(string.Join(Environment.NewLine, jaggedMatrix.Select(r => string.Join(" ", r))));
  28.         }
  29.  
  30.         public static void AddOrSubtract(double[][] jaggedMatrix)
  31.         {
  32.             string command = Console.ReadLine();
  33.             while (command?.ToLower() != "end")
  34.             {
  35.                 string[] cmdArr = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  36.                 string action = cmdArr[0];
  37.                 int row = int.Parse(cmdArr[1]);
  38.                 int col = int.Parse(cmdArr[2]);
  39.                 int value = int.Parse(cmdArr[3]);
  40.                 switch (action)
  41.                 {
  42.                     case "Add":
  43.                         if (Validation2(row, col, jaggedMatrix))
  44.                         {
  45.                             jaggedMatrix[row][col] += value;
  46.                         }
  47.  
  48.                         break;
  49.                     case "Subtract":
  50.                         if (Validation2(row, col, jaggedMatrix))
  51.                         {
  52.                             jaggedMatrix[row][col] -= value;
  53.                         }
  54.  
  55.                         break;
  56.                 }
  57.  
  58.                 command = Console.ReadLine();
  59.             }
  60.         }
  61.  
  62.         public static void FillTheJaggedMatrix(int rows, double[][] jaggedMatrix)
  63.         {
  64.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  65.             {
  66.                 double[] numbersToFill = Console.ReadLine()
  67.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  68.                     .Select(double.Parse)
  69.                     .ToArray();
  70.  
  71.                 jaggedMatrix[rowIndex] = numbersToFill;
  72.             }
  73.         }
  74.  
  75.         public static void CheckForEqualsRows(double[][] jaggedMatrix)
  76.         {
  77.             for (int rowIndex = 0; rowIndex < jaggedMatrix.GetLength(0); rowIndex++)
  78.             {
  79.                 int currentRowLength = jaggedMatrix[rowIndex].Length;
  80.                 bool isValid = Validation(rowIndex + 1, jaggedMatrix);
  81.  
  82.                 if (isValid)
  83.                 {
  84.                     if (currentRowLength == jaggedMatrix[rowIndex + 1].Length)
  85.                     {
  86.                         for (int row = 0; row < jaggedMatrix[rowIndex].Length; row++)
  87.                         {
  88.                             jaggedMatrix[rowIndex][row] *= 2;
  89.                             jaggedMatrix[rowIndex + 1][row] *= 2;
  90.                         }
  91.                     }
  92.                     else
  93.                     {
  94.                         for (int row = 0; row < jaggedMatrix[rowIndex].Length; row++)
  95.                         {
  96.                             jaggedMatrix[rowIndex][row] /= 2;
  97.                         }
  98.  
  99.                         for (int rowLength = 0; rowLength < jaggedMatrix[rowIndex + 1].Length; rowLength++)
  100.                         {
  101.                             jaggedMatrix[rowIndex + 1][rowLength] /= 2;
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.  
  108.         public static bool Validation(int rows,double[][] jaggedMatrix)
  109.         {
  110.             bool isValidIndexes = rows >= 0 && rows < jaggedMatrix.GetLength(0);
  111.  
  112.             return isValidIndexes;
  113.         }
  114.  
  115.         public static bool Validation2(int rows, int cols, double[][] jaggedMatrix)
  116.         {
  117.             bool isValidIndexes = rows >= 0 && rows <= jaggedMatrix.GetLength(0)
  118.                                             && cols >= 0 && cols < jaggedMatrix[rows].Length;
  119.  
  120.             return isValidIndexes;
  121.         }
  122.     }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment