Advertisement
Guest User

Jagged Array Manipulator

a guest
Sep 24th, 2019
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Jagged_Array_Manipulator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int rows = int.Parse(Console.ReadLine());
  14.             if (rows==0)
  15.             {
  16.                 return;
  17.             }
  18.             var matrix = new double[5][];
  19.             for (int row = 0; row < rows; row++)
  20.             {
  21.                 var currentRow = Console.ReadLine()
  22.                     .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  23.                     .Select(double.Parse)
  24.                     .ToArray();
  25.                     matrix[row] = new double[currentRow.Length];
  26.                 for (int col = 0; col < currentRow.Length; col++)
  27.                 {
  28.                     matrix[row][col] = currentRow[col];
  29.                 }
  30.             }
  31.             for (int row = 0; row < rows-1; row++)
  32.             {
  33.                 if (matrix[row].Length==matrix[row+1].Length)
  34.                 {
  35.                     for (int rowCurrent = row; rowCurrent <=row+1 ; rowCurrent++)
  36.                     {
  37.                         for (int col = 0; col < matrix[rowCurrent].Length; col++)
  38.                         {
  39.                             matrix[rowCurrent][col] = matrix[rowCurrent][col]*2;
  40.                         }
  41.                     }
  42.                 }
  43.                 else
  44.                 {
  45.                     for (int rowCurrent  = row; rowCurrent <=row + 1; rowCurrent++)
  46.                     {
  47.                         for (int col = 0; col < matrix[rowCurrent].Length; col++)
  48.                         {
  49.                             matrix[rowCurrent][col] = matrix[rowCurrent][col] *0.5 ;
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.             while (true)
  55.             {
  56.                 string command = Console.ReadLine();
  57.                 if (command.ToLower()=="end")
  58.                 {
  59.                     break;
  60.                 }
  61.                 string[] commandParts = command
  62.                     .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  63.  
  64.                 if (commandParts.Length !=4 )
  65.                 {
  66.                     continue;
  67.                 }
  68.  
  69.                 string commandArgs = commandParts[0];
  70.                 int row = int.Parse(commandParts[1]);
  71.                 int col = int.Parse(commandParts[2]);
  72.                 double value = double.Parse(commandParts[3]);
  73.  
  74.                 if (commandArgs.ToLower()=="add")
  75.                 {
  76.                     if (row<0 || row>rows || col<0  ||  col>matrix[row].Length )
  77.                     {
  78.                         continue;
  79.                     }
  80.                     matrix[row][col] += value;
  81.                 }
  82.                 else if(commandArgs.ToLower()=="subtract")
  83.                 {
  84.                     if (row < 0 || row > rows || col < 0 || col > matrix[row].Length)
  85.                     {
  86.                         continue;
  87.                     }
  88.                     matrix[row][col] -= value;
  89.                 }
  90.                  
  91.             }
  92.            for (int row = 0; row < rows; row++)
  93.            {
  94.                for (int col = 0; col < matrix[row].Length; col++)
  95.                {
  96.                    Console.Write(matrix[row][col]+" ");
  97.                }
  98.                Console.WriteLine( );
  99.            }
  100.          
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement