Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- namespace _6._Jagged_Array_Manipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- int rows = int.Parse(Console.ReadLine());
- double[][] jaggedMatrix = new double[rows][];
- FillTheJaggedMatrix(rows, jaggedMatrix);
- CheckForEqualsRows(jaggedMatrix);
- // • "Add {row} {column} {value}" - add { value}
- //to the element at the given indexes, if they are valid
- // • "Subtract {row} {column} {value}" - subtract { value}
- //from the element at the given indexes, if they are valid
- // //• "End" - print the final state of the matrix(all elements separated by a single space) and stop the program
- AddOrSubtract(jaggedMatrix);
- Console.WriteLine(string.Join(Environment.NewLine, jaggedMatrix.Select(r => string.Join(" ", r))));
- }
- public static void AddOrSubtract(double[][] jaggedMatrix)
- {
- string command = Console.ReadLine();
- while (command?.ToLower() != "end")
- {
- string[] cmdArr = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- string action = cmdArr[0];
- int row = int.Parse(cmdArr[1]);
- int col = int.Parse(cmdArr[2]);
- int value = int.Parse(cmdArr[3]);
- switch (action)
- {
- case "Add":
- if (Validation2(row, col, jaggedMatrix))
- {
- jaggedMatrix[row][col] += value;
- }
- break;
- case "Subtract":
- if (Validation2(row, col, jaggedMatrix))
- {
- jaggedMatrix[row][col] -= value;
- }
- break;
- }
- command = Console.ReadLine();
- }
- }
- public static void FillTheJaggedMatrix(int rows, double[][] jaggedMatrix)
- {
- for (int rowIndex = 0; rowIndex < rows; rowIndex++)
- {
- double[] numbersToFill = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(double.Parse)
- .ToArray();
- jaggedMatrix[rowIndex] = numbersToFill;
- }
- }
- public static void CheckForEqualsRows(double[][] jaggedMatrix)
- {
- for (int rowIndex = 0; rowIndex < jaggedMatrix.GetLength(0); rowIndex++)
- {
- int currentRowLength = jaggedMatrix[rowIndex].Length;
- bool isValid = Validation(rowIndex + 1, jaggedMatrix);
- if (isValid)
- {
- if (currentRowLength == jaggedMatrix[rowIndex + 1].Length)
- {
- for (int row = 0; row < jaggedMatrix[rowIndex].Length; row++)
- {
- jaggedMatrix[rowIndex][row] *= 2;
- jaggedMatrix[rowIndex + 1][row] *= 2;
- }
- }
- else
- {
- for (int row = 0; row < jaggedMatrix[rowIndex].Length; row++)
- {
- jaggedMatrix[rowIndex][row] /= 2;
- }
- for (int rowLength = 0; rowLength < jaggedMatrix[rowIndex + 1].Length; rowLength++)
- {
- jaggedMatrix[rowIndex + 1][rowLength] /= 2;
- }
- }
- }
- }
- }
- public static bool Validation(int rows,double[][] jaggedMatrix)
- {
- bool isValidIndexes = rows >= 0 && rows < jaggedMatrix.GetLength(0);
- return isValidIndexes;
- }
- public static bool Validation2(int rows, int cols, double[][] jaggedMatrix)
- {
- bool isValidIndexes = rows >= 0 && rows <= jaggedMatrix.GetLength(0)
- && cols >= 0 && cols < jaggedMatrix[rows].Length;
- return isValidIndexes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment