-1

The program is supposed to shift all values in the array to the right. Please help with what I have done wrong...

    int[] array = new int[] {1, 2, 3, 4, 5};
    int[] rotated = new int [array.length];

    int rotationTimes = scan.nextInt();
    if (rotationTimes > 4){
        rotationTimes %= 5;
    }

    for (int i = 0; i <= rotationTimes; i++){
        if (i == 4) {
            rotated[0] = array[4];
            break;
        }
        array[i] = rotated[i + 1];
    }

    System.out.println(Arrays.toString(rotated)); 

The expected output should be {5, 1, 2, 3, 4}. It outputs an array of 0s... Thank you a lot.

1 Answers1

0
  1. You weren't populating the rotated array. You were modifying the array reference!
  2. You were checking if index == 4 when you could do array.length - 1.
  3. You need to determine the target index as being (index + rotationTimes) then if it exceeds array.length - 1 (the last index) you use the modulus operator to make sure it doesnt overflow. For example, if the current index is 4 (the last one) and the iterations is 1, (4 + 1) is greater than 4 so the target index is (4 + 1) % 5 = 0.
    public static void main(String[] args) {
        int[] array = new int[] {1, 2, 3, 4, 5};

        int[] rotated = new int [array.length];

        System.out.println("Enter amount of times to rotate: ");
        Scanner scan = new Scanner(System.in);

        int rotationTimes = scan.nextInt();

        if (rotationTimes > 4){
            rotationTimes %= 5;
        }

        for (int index = 0; index < array.length; index++) {
            int targetIndex = index + rotationTimes;

            if (targetIndex > array.length - 1) {
                targetIndex %= array.length; 
            }
            rotated[targetIndex] = array[index];
        }

        System.out.println("Input:  " + Arrays.toString(array));
        System.out.println("Rotations: " + rotationTimes);
        System.out.println("Output: " + Arrays.toString(rotated));
    }
Input:  [1, 2, 3, 4, 5]
Rotations: 1
Output: [5, 1, 2, 3, 4]
Input:  [1, 2, 3, 4, 5]
Rotations: 2
Output: [4, 5, 1, 2, 3]
Jason
  • 5,154
  • 2
  • 12
  • 22