Coolb

4.Array Rotation

Oct 13th, 2018
1,576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _04._Array_Rotation
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             //  4.Array Rotation
  11.             //Write a program that receives an array and number of rotations you have to perform(first element goes at the end) Print the resulting array.
  12.                        
  13.             int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.             int numberRotations = int.Parse(Console.ReadLine());
  15.  
  16.             if (numberRotations<array.Length)
  17.             {
  18.                 numberRotations=numberRotations;    // <------ here VS dont like it so much ! :( why and how to solve the problem?
  19.             }
  20.             else if (numberRotations==array.Length)
  21.             {
  22.                 Console.WriteLine(string.Join(" ", array));                
  23.                 return;
  24.             }
  25.             else if (numberRotations>array.Length)
  26.             {
  27.                 numberRotations = numberRotations - array.Length;
  28.             }
  29.  
  30.             int[] b = new int[numberRotations];
  31.             Array.Copy(array, 0, b, 0, numberRotations);
  32.  
  33.             int[] c = new int[array.Length - numberRotations];
  34.             Array.Copy(array, numberRotations, c, 0, array.Length - numberRotations);
  35.            
  36.             Console.WriteLine($"{String.Concat(String.Join(" ", c))} {String.Concat(String.Join(" ", b))}");
  37.  
  38.             //String.Join(" ", c);
  39.             //Array.Copy(a, 1, b, 0, 3);
  40.             //a = source array
  41.             //1 = start index in source array
  42.             //b = destination array
  43.             //0 = start index in destination array
  44.             //3 = elements to copy
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment