Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Runtime.ExceptionServices;
- using System.Threading;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("----WELCOME TO ARRAY MANIPULATOR----");
- Console.WriteLine("----HERE ARE THE COMMANDS YOU CAN USE TO MANIPULATE YOUR ARRAY----");
- 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]");
- Console.WriteLine("max even / odd– returns the INDEX of the max even / odd element-> [1, 4, 8, 2, 3]->max odd->print 4");
- Console.WriteLine("min even / odd – returns the INDEX of the min even / odd element-> [1, 4, 8, 2, 3]->min even > print 3");
- Console.WriteLine("first {count} even / odd– returns the first { count} elements-> [1, 8, 2, 3]->first 2 even->print[8, 2]");
- Console.WriteLine("last { count} even / odd – returns the last { count} elements-> [1, 8, 2, 3]->last 2 odd->print[1, 3]");
- Console.WriteLine("end – stop taking input and print the final state of the array");
- Console.WriteLine("-----------------------------------------------------------------");
- Console.Write("Enter your array here: ");
- int[] array = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
- CommandTaker(array);
- }
- static void CommandTaker(int[] array)
- {
- string command;
- while((command = Console.ReadLine()) != "end")
- {
- string[] commandSplit = command.Split(" ");
- if (commandSplit[0].Equals("exchange"))
- {
- ExchangeArray(array,commandSplit[1]);
- }
- else if(command.Equals("max even"))
- {
- MaxEven(array);
- }
- else if(command.Equals("max odd"))
- {
- MaxOdd(array);
- }
- else if (commandSplit[0].Equals("first"))
- {
- if (commandSplit[2].Equals("even"))
- {
- FirstEven(int.Parse(commandSplit[1]),array);
- }
- else if (commandSplit[2].Equals("odd"))
- {
- FirstOdd(int.Parse(commandSplit[1]), array);
- }
- }
- else if (commandSplit[0].Equals("last"))
- {
- if (commandSplit[2].Equals("even"))
- {
- LastEven(int.Parse(commandSplit[1]), array);
- }
- else if (commandSplit[2].Equals("odd"))
- {
- LastOdd(int.Parse(commandSplit[1]), array);
- }
- }
- else if (command.Equals("min even"))
- {
- MinEven(array);
- }
- else if (command.Equals("min odd"))
- {
- MinOdd(array);
- }
- else
- {
- Console.WriteLine("Invalid command.");
- }
- }
- }
- private static void LastOdd(int number, int[] array)
- {
- int counter = number;
- if (counter > array.Length)
- {
- Console.WriteLine("Invalid count!");
- }
- else
- {
- for (int i = array.Length - 1; i > 0; i--)
- {
- if (array[i] % 2 != 0 && counter > 0)
- {
- Console.Write(array[i] + " ");
- counter--;
- }
- }
- Console.WriteLine();
- }
- }
- private static void LastEven(int number, int[] array)
- {
- int counter = number;
- if (counter > array.Length)
- {
- Console.WriteLine("Invalid count!");
- }
- else
- {
- for (int i = array.Length - 1; i > 0; i--)
- {
- if (array[i] % 2 == 0 && counter > 0)
- {
- Console.Write(array[i] + " ");
- counter--;
- }
- }
- Console.WriteLine();
- }
- }
- private static void FirstEven(int number,int[] array)
- {
- int counter = number;
- if (counter > array.Length)
- {
- Console.WriteLine("Invalid count!");
- }
- else
- {
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && counter > 0)
- {
- Console.Write(array[i] + " ");
- counter--;
- }
- }
- Console.WriteLine();
- }
- }
- private static void FirstOdd(int number, int[] array)
- {
- int counter = number;
- if (counter > array.Length)
- {
- Console.WriteLine("Invalid count!");
- }
- else
- {
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && counter > 0)
- {
- Console.Write(array[i] + " ");
- counter--;
- }
- }
- Console.WriteLine();
- }
- }
- private static void MaxOdd(int[] array)
- {
- int maxNum = int.MinValue;
- int index = 0;
- bool isIt = false;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && array[i] >= maxNum)
- {
- maxNum = array[i];
- index = i;
- isIt = true;
- }
- }
- if (isIt == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(index);
- }
- }
- private static void MinOdd(int[] array)
- {
- int minNum = int.MaxValue;
- int index = 0;
- bool isIt = false;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 != 0 && array[i] <= minNum)
- {
- minNum = array[i];
- index = i;
- isIt = true;
- }
- }
- if (isIt == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(index);
- }
- }
- private static void MaxEven(int[] array)
- {
- int maxNum = int.MinValue;
- int index = 0;
- bool isIt = false;
- for(int i = 0; i < array.Length; i++)
- {
- if(array[i] % 2 == 0 && array[i] >= maxNum)
- {
- maxNum = array[i];
- index = i;
- isIt = true;
- }
- }
- if(isIt == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(index);
- }
- }
- private static void MinEven(int[] array)
- {
- int minNum = int.MaxValue;
- int index = 0;
- bool isIt = false;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] % 2 == 0 && array[i] <= minNum)
- {
- minNum = array[i];
- index = i;
- isIt = true;
- }
- }
- if (isIt == false)
- {
- Console.WriteLine("No matches");
- }
- else
- {
- Console.WriteLine(index);
- }
- }
- //The exchange method is ready
- private static void ExchangeArray(int[] arr,string number)
- {
- int exchangePoint = int.Parse(number);
- if(exchangePoint > arr.Length - 1)
- {
- Console.WriteLine("Invalid index!Outside of array boundaries.");
- }
- else
- {
- int[] assistArr = new int[arr.Length];
- int count = 0;
- for(int i = exchangePoint+1; i < arr.Length; i++)
- {
- assistArr[count] = arr[i];
- count++;
- }
- for(int k = 0; k <= exchangePoint; k++)
- {
- assistArr[count] = arr[k];
- count++;
- }
- for(int s = 0; s < arr.Length; s++)
- {
- arr[s] = assistArr[s];
- Console.Write(arr[s] + " ");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment