Advertisement
Guest User

Untitled

a guest
Sep 17th, 2021
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace JaggedArrayModification
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int rowsLength = int.Parse(Console.ReadLine());
  11.  
  12.             int[][] jaggedArray = new int[rowsLength][];
  13.  
  14.             for (int row = 0; row < jaggedArray.Length; row++)
  15.             {
  16.                 int[] elements = Console.ReadLine().Split().Select(int.Parse).ToArray();
  17.                 jaggedArray[row] = new int[elements.Length];
  18.  
  19.                 for (int col = 0; col < jaggedArray[row].Length; col++)
  20.                 {
  21.                     jaggedArray[row][col] = elements[col];
  22.                 }
  23.             }
  24.  
  25.             string[] command = Console.ReadLine().Split();
  26.  
  27.             while (command[0] != "END")
  28.             {
  29.                 int row = 0;
  30.                 int col = 0;
  31.                 int value = 0;
  32.                
  33.                 row = int.Parse(command[1]);
  34.                 col = int.Parse(command[2]);
  35.                 value = int.Parse(command[3]);
  36.  
  37.                 if (row < 0 || row >= jaggedArray.Length || col < 0 || col >= jaggedArray[row].Length)
  38.                 {
  39.                     Console.WriteLine("Invalid coordinates");
  40.                     command = Console.ReadLine().Split();
  41.                     continue;
  42.                 }      
  43.  
  44.                 if (command[0] == "Add")
  45.                 {
  46.                     jaggedArray[row][col] += value;
  47.                 }
  48.                 else if (command[0] == "Subtract")
  49.                 {
  50.                     jaggedArray[row][col] -= value;
  51.                 }
  52.  
  53.                 command = Console.ReadLine().Split();
  54.             }
  55.  
  56.             for (int row = 0; row < jaggedArray.Length; row++)
  57.             {
  58.                 Console.WriteLine(String.Join(" ", jaggedArray[row]));
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement