Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Zack
- * ICE 11
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ice11
- {
- class Program
- {
- static void Main(string[] args)
- {
- // title
- Console.Title = " ICE 11";
- // Variables
- int shifts; // Shift how many indexes
- string[] elements; // inpuit array
- bool isValid; // valiation
- bool restart = true; // if user wants to restart
- //If the user wants to restart
- do
- {
- Console.Clear();
- Console.WriteLine(@"
- ===============
- = Array =
- ===============
- ");
- // Ask for # o elements
- Console.Write(" enter # of elements divided by commas: ");
- // Split input into an array of string
- Console.ForegroundColor = ConsoleColor.Cyan;
- elements = Console.ReadLine().Split(',');
- Console.ForegroundColor = ConsoleColor.Gray;
- // Whole number validation do-while
- do
- {
- Console.Write("Enter shifts: ");
- Console.ForegroundColor = ConsoleColor.Cyan;
- isValid = int.TryParse(Console.ReadLine(), out shifts);
- Console.ForegroundColor = ConsoleColor.Gray;
- // Tryparse is false if input is not whole number
- if(!isValid)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Error shift must be a whole number...");
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- } while (!isValid);
- // Convert negative shifts into positive
- // Add the element.Length to the # o shifts
- if (shifts < 0) shifts += elements.Length;
- //avoid rotating more than nessecary
- // ex shifts = 12 and I / by length 5 = remainder 2 shifts left
- shifts %= elements.Length;
- // loop as many times as given shifts
- for(int countShifts = 0; countShifts < shifts; countShifts++)
- {
- string dummy = elements[0]; // copy first element
- // SHift array
- for (int countElements = 0; countElements < elements.Length - 1; countElements++)
- {
- elements[countElements] = elements[countElements + 1];
- }
- // Copy first element to last index
- elements[elements.Length - 1] = dummy;
- }
- // Print out shifted array
- Console.Write("\nShifted Array");
- Console.ForegroundColor = ConsoleColor.Cyan;
- foreach (string element in elements)
- {
- Console.Write(element + " ");
- }
- Console.ForegroundColor = ConsoleColor.Gray;
- // ask for restart or exit
- Console.Write("\n\nPress [y] to restart [any] to exit...");
- restart = Console.ReadKey().KeyChar == 'y';
- } while (restart);
- // exit app
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement