Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. /*
  2. * Zack
  3. * ICE 11
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ice11
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. // title
  18. Console.Title = " ICE 11";
  19.  
  20. // Variables
  21. int shifts; // Shift how many indexes
  22. string[] elements; // inpuit array
  23. bool isValid; // valiation
  24. bool restart = true; // if user wants to restart
  25.  
  26. //If the user wants to restart
  27. do
  28. {
  29.  
  30. Console.Clear();
  31.  
  32. Console.WriteLine(@"
  33. ===============
  34. = Array =
  35. ===============
  36. ");
  37. // Ask for # o elements
  38. Console.Write(" enter # of elements divided by commas: ");
  39.  
  40. // Split input into an array of string
  41. Console.ForegroundColor = ConsoleColor.Cyan;
  42. elements = Console.ReadLine().Split(',');
  43. Console.ForegroundColor = ConsoleColor.Gray;
  44.  
  45. // Whole number validation do-while
  46. do
  47. {
  48. Console.Write("Enter shifts: ");
  49. Console.ForegroundColor = ConsoleColor.Cyan;
  50. isValid = int.TryParse(Console.ReadLine(), out shifts);
  51. Console.ForegroundColor = ConsoleColor.Gray;
  52.  
  53. // Tryparse is false if input is not whole number
  54. if(!isValid)
  55. {
  56. Console.ForegroundColor = ConsoleColor.Red;
  57. Console.WriteLine("Error shift must be a whole number...");
  58. Console.ForegroundColor = ConsoleColor.Gray;
  59.  
  60. }
  61.  
  62. } while (!isValid);
  63.  
  64. // Convert negative shifts into positive
  65. // Add the element.Length to the # o shifts
  66. if (shifts < 0) shifts += elements.Length;
  67.  
  68. //avoid rotating more than nessecary
  69. // ex shifts = 12 and I / by length 5 = remainder 2 shifts left
  70.  
  71. shifts %= elements.Length;
  72.  
  73. // loop as many times as given shifts
  74. for(int countShifts = 0; countShifts < shifts; countShifts++)
  75. {
  76. string dummy = elements[0]; // copy first element
  77.  
  78. // SHift array
  79. for (int countElements = 0; countElements < elements.Length - 1; countElements++)
  80. {
  81. elements[countElements] = elements[countElements + 1];
  82. }
  83.  
  84.  
  85. // Copy first element to last index
  86. elements[elements.Length - 1] = dummy;
  87. }
  88.  
  89. // Print out shifted array
  90. Console.Write("\nShifted Array");
  91. Console.ForegroundColor = ConsoleColor.Cyan;
  92. foreach (string element in elements)
  93. {
  94. Console.Write(element + " ");
  95. }
  96.  
  97. Console.ForegroundColor = ConsoleColor.Gray;
  98. // ask for restart or exit
  99. Console.Write("\n\nPress [y] to restart [any] to exit...");
  100. restart = Console.ReadKey().KeyChar == 'y';
  101.  
  102. } while (restart);
  103.  
  104. // exit app
  105.  
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement