C program to right rotate an array

Category: C Program

Learn how to write a C program to right rotate an array. This article provides a detailed explanation and sample code for right rotating array elements using a simple iterative approach.

Right rotating an array involves shifting the elements of the array to the right by a specified number of positions. This operation is useful in various applications such as data manipulation, cyclic shifts, and algorithm implementations. In this article, we will guide you through writing a C program to right rotate an array, providing a detailed explanation and sample code.

Steps to Right Rotate an Array

To right rotate an array, we can follow these steps:

  1. Input the Array: Read the array elements from the user.
  2. Input the Number of Rotations: Read the number of positions to rotate the array to the right.
  3. Perform the Rotation: Shift the elements of the array to the right and move the last elements to the beginning of the array.
  4. Print the Result: Output the rotated array to the console.

Write a C program to right rotate an array

#include <stdio.h>

void rightRotate(int arr[], int size, int rotations) {
    int temp[rotations];

    // Adjust the number of rotations if it is greater than the size of the array
    rotations = rotations % size;

    // Store the last 'rotations' elements in a temporary array
    for (int i = 0; i < rotations; i++) {
        temp[i] = arr[size - rotations + i];
    }

    // Shift the rest of the array elements to the right
    for (int i = size - 1; i >= rotations; i--) {
        arr[i] = arr[i - rotations];
    }

    // Move the temporary array elements to the beginning of the array
    for (int i = 0; i < rotations; i++) {
        arr[i] = temp[i];
    }
}

int main() {
    int size, rotations;

    // Input size and elements of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);
    int arr[size];
    printf("Enter %d elements -\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    // Input the number of rotations
    printf("\nEnter the number of positions to rotate the array to the right: ");
    scanf("%d", &rotations);

    // Perform the right rotation
    rightRotate(arr, size, rotations);

    // Print the rotated array
    printf("Array after right rotation: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Output

Enter the size of the array: 5
Enter 5 elements -
3
5
6
2
1

Enter the number of positions to rotate the array to the right: 3
Array after right rotation: 6 2 1 3 5

Explanation

  1. Input the Array: The user inputs the size of the array and its elements.
  2. Input the Number of Rotations: The user inputs the number of positions to rotate the array to the right.
  3. Perform the Rotation:
    • Adjust the number of rotations if it is greater than the size of the array.
    • The last 'rotations' elements are stored in a temporary array.
    • The remaining elements are shifted to the right.
    • The elements in the temporary array are moved to the beginning of the array.
  4. Print the Result: The program prints the elements of the rotated array.

Detailed Steps

  • Step 1: Input the Array
    • The program prompts the user to enter the size of the array.
    • The user inputs the array elements.
  • Step 2: Input the Number of Rotations
    • The program prompts the user to enter the number of positions to rotate the array to the right.
  • Step 3: Perform the Rotation
    • Adjust Rotations: If the number of rotations is greater than the size of the array, it is adjusted using modulo operation to avoid unnecessary full rotations.
    • Store Elements in Temporary Array: The last 'rotations' elements are stored in a temporary array.
    • Shift Elements Right: A loop runs from the end of the array to the beginning, shifting each element to the right by the number of rotations.
    • Move Temporary Elements: Another loop runs to copy the elements from the temporary array to the beginning of the original array.
  • Step 4: Print the Result
    • The program prints the elements of the rotated array.

Recommended Posts