TheBulgarianWolf

Array Manipulation

Nov 1st, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.51 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Runtime.ExceptionServices;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("----WELCOME TO ARRAY MANIPULATOR----");
  14.             Console.WriteLine("----HERE ARE THE COMMANDS YOU CAN USE TO MANIPULATE YOUR ARRAY----");
  15.             Console.WriteLine("exchange {index} – splits the array after the given index, and exchanges the places of the two resulting sub-arrays. E.g. [1, 2, 3, 4, 5] -> exchange 2 -> result: [4, 5, 1, 2, 3]");
  16.             Console.WriteLine("max even / odd– returns the INDEX of the max even / odd element-> [1, 4, 8, 2, 3]->max odd->print 4");
  17.             Console.WriteLine("min even / odd – returns the INDEX of the min even / odd element-> [1, 4, 8, 2, 3]->min even > print 3");
  18.             Console.WriteLine("first {count} even / odd– returns the first { count} elements-> [1, 8, 2, 3]->first 2 even->print[8, 2]");
  19.             Console.WriteLine("last { count} even / odd – returns the last { count} elements-> [1, 8, 2, 3]->last 2 odd->print[1, 3]");
  20.             Console.WriteLine("end – stop taking input and print the final state of the array");
  21.             Console.WriteLine("-----------------------------------------------------------------");
  22.             Console.Write("Enter your array here: ");
  23.             int[] array = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  24.             CommandTaker(array);
  25.  
  26.         }
  27.  
  28.         static void CommandTaker(int[] array)
  29.         {
  30.             string command;
  31.             while((command = Console.ReadLine()) != "end")
  32.             {
  33.                 string[] commandSplit = command.Split(" ");
  34.                 if (commandSplit[0].Equals("exchange"))
  35.                 {
  36.                     ExchangeArray(array,commandSplit[1]);
  37.                 }
  38.                 else if(command.Equals("max even"))
  39.                 {
  40.                     MaxEven(array);
  41.                 }
  42.                 else if(command.Equals("max odd"))
  43.                 {
  44.                     MaxOdd(array);
  45.                 }
  46.                 else if (commandSplit[0].Equals("first"))
  47.                 {
  48.                     if (commandSplit[2].Equals("even"))
  49.                     {
  50.                         FirstEven(int.Parse(commandSplit[1]),array);
  51.                     }
  52.                     else if (commandSplit[2].Equals("odd"))
  53.                     {
  54.                         FirstOdd(int.Parse(commandSplit[1]), array);
  55.                     }
  56.                 }
  57.                 else if (commandSplit[0].Equals("last"))
  58.                 {
  59.                     if (commandSplit[2].Equals("even"))
  60.                     {
  61.                         LastEven(int.Parse(commandSplit[1]), array);
  62.                     }
  63.                     else if (commandSplit[2].Equals("odd"))
  64.                     {
  65.                         LastOdd(int.Parse(commandSplit[1]), array);
  66.                     }
  67.                 }
  68.                 else if (command.Equals("min even"))
  69.                 {
  70.                     MinEven(array);
  71.                 }
  72.                 else if (command.Equals("min odd"))
  73.                 {
  74.                     MinOdd(array);
  75.                 }
  76.                 else
  77.                 {
  78.                     Console.WriteLine("Invalid command.");
  79.                 }
  80.  
  81.             }
  82.         }
  83.  
  84.         private static void LastOdd(int number, int[] array)
  85.         {
  86.             int counter = number;
  87.             if (counter > array.Length)
  88.             {
  89.                 Console.WriteLine("Invalid count!");
  90.             }
  91.             else
  92.             {
  93.                 for (int i = array.Length - 1; i > 0; i--)
  94.                 {
  95.                     if (array[i] % 2 != 0 && counter > 0)
  96.                     {
  97.                         Console.Write(array[i] + " ");
  98.                         counter--;
  99.                     }
  100.  
  101.                 }
  102.                 Console.WriteLine();
  103.             }
  104.         }
  105.  
  106.         private static void LastEven(int number, int[] array)
  107.         {
  108.             int counter = number;
  109.             if (counter > array.Length)
  110.             {
  111.                 Console.WriteLine("Invalid count!");
  112.             }
  113.             else
  114.             {
  115.                 for (int i = array.Length - 1; i > 0; i--)
  116.                 {
  117.                     if (array[i] % 2 == 0 && counter > 0)
  118.                     {
  119.                         Console.Write(array[i] + " ");
  120.                         counter--;
  121.                     }
  122.  
  123.                 }
  124.                 Console.WriteLine();
  125.             }
  126.         }
  127.  
  128.         private static void FirstEven(int number,int[] array)
  129.         {
  130.             int counter = number;
  131.             if (counter > array.Length)
  132.             {
  133.                 Console.WriteLine("Invalid count!");
  134.             }
  135.             else
  136.             {
  137.                 for (int i = 0; i < array.Length; i++)
  138.                 {
  139.                     if (array[i] % 2 == 0 && counter > 0)
  140.                     {
  141.                         Console.Write(array[i] + " ");
  142.                         counter--;
  143.                     }
  144.  
  145.                 }
  146.                 Console.WriteLine();
  147.             }
  148.         }
  149.  
  150.         private static void FirstOdd(int number, int[] array)
  151.         {
  152.             int counter = number;
  153.             if (counter > array.Length)
  154.             {
  155.                 Console.WriteLine("Invalid count!");
  156.             }
  157.             else
  158.             {
  159.                 for (int i = 0; i < array.Length; i++)
  160.                 {
  161.                     if (array[i] % 2 != 0 && counter > 0)
  162.                     {
  163.                         Console.Write(array[i] + " ");
  164.                         counter--;
  165.                     }
  166.  
  167.                 }
  168.                 Console.WriteLine();
  169.             }
  170.         }
  171.  
  172.         private static void MaxOdd(int[] array)
  173.         {
  174.             int maxNum = int.MinValue;
  175.             int index = 0;
  176.             bool isIt = false;
  177.             for (int i = 0; i < array.Length; i++)
  178.             {
  179.                 if (array[i] % 2 != 0 && array[i] >= maxNum)
  180.                 {
  181.                     maxNum = array[i];
  182.                     index = i;
  183.                     isIt = true;
  184.                 }
  185.             }
  186.  
  187.             if (isIt == false)
  188.             {
  189.                 Console.WriteLine("No matches");
  190.             }
  191.             else
  192.             {
  193.                 Console.WriteLine(index);
  194.             }
  195.         }
  196.  
  197.         private static void MinOdd(int[] array)
  198.         {
  199.             int minNum = int.MaxValue;
  200.             int index = 0;
  201.             bool isIt = false;
  202.             for (int i = 0; i < array.Length; i++)
  203.             {
  204.                 if (array[i] % 2 != 0 && array[i] <= minNum)
  205.                 {
  206.                     minNum = array[i];
  207.                     index = i;
  208.                     isIt = true;
  209.                 }
  210.             }
  211.  
  212.             if (isIt == false)
  213.             {
  214.                 Console.WriteLine("No matches");
  215.             }
  216.             else
  217.             {
  218.                 Console.WriteLine(index);
  219.             }
  220.         }
  221.  
  222.         private static void MaxEven(int[] array)
  223.         {
  224.             int maxNum = int.MinValue;
  225.             int index = 0;
  226.             bool isIt = false;
  227.             for(int i = 0; i < array.Length; i++)
  228.             {
  229.                 if(array[i] % 2 == 0 && array[i] >= maxNum)
  230.                 {
  231.                     maxNum = array[i];
  232.                     index = i;
  233.                     isIt = true;
  234.                 }
  235.             }
  236.              if(isIt == false)
  237.             {
  238.                 Console.WriteLine("No matches");
  239.             }
  240.             else
  241.             {
  242.                 Console.WriteLine(index);
  243.             }
  244.            
  245.         }
  246.  
  247.         private static void MinEven(int[] array)
  248.         {
  249.             int minNum = int.MaxValue;
  250.             int index = 0;
  251.             bool isIt = false;
  252.             for (int i = 0; i < array.Length; i++)
  253.             {
  254.                 if (array[i] % 2 == 0 && array[i] <= minNum)
  255.                 {
  256.                     minNum = array[i];
  257.                     index = i;
  258.                     isIt = true;
  259.                 }
  260.             }
  261.  
  262.             if (isIt == false)
  263.             {
  264.                 Console.WriteLine("No matches");
  265.             }
  266.             else
  267.             {
  268.                 Console.WriteLine(index);
  269.             }
  270.         }
  271.  
  272.  
  273.         //The exchange method is ready
  274.         private static void ExchangeArray(int[] arr,string number)
  275.         {
  276.             int exchangePoint = int.Parse(number);
  277.             if(exchangePoint > arr.Length - 1)
  278.             {
  279.                 Console.WriteLine("Invalid index!Outside of array boundaries.");
  280.             }
  281.             else
  282.             {
  283.                 int[] assistArr = new int[arr.Length];
  284.                 int count = 0;
  285.                 for(int i = exchangePoint+1; i < arr.Length; i++)
  286.                 {
  287.                     assistArr[count] = arr[i];
  288.                     count++;
  289.                 }
  290.                
  291.                 for(int k = 0; k <= exchangePoint; k++)
  292.                 {
  293.                     assistArr[count] = arr[k];
  294.                     count++;
  295.                 }
  296.  
  297.                 for(int s = 0; s < arr.Length; s++)
  298.                 {
  299.                     arr[s] = assistArr[s];
  300.                     Console.Write(arr[s] + " ");
  301.                 }
  302.             }
  303.         }
  304.     }
  305. }
  306.  
Add Comment
Please, Sign In to add comment