C program to left rotate an array

Category: C Program

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

Rotating an array is a common task in C programming that involves shifting the elements of the array in a specific direction. In this article, we will focus on left rotation, which moves each element of the array to the left by a specified number of positions. This operation is useful in various applications such as data manipulation, cyclic shifts, and algorithm implementations.

Steps to Left Rotate an Array

To left 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 left.
  3. Perform the Rotation: Shift the elements of the array to the left and move the first elements to the end of the array.
  4. Print the Result: Output the rotated array to the console.

Write a C program to left rotate an array

#include <stdio.h>

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

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

    // Shift the rest of the array elements to the left
    for (int i = 0; i < size - rotations; i++) {
        arr[i] = arr[i + rotations];
    }

    // Move the temporary array elements to the end of the array
    for (int i = 0; i < rotations; i++) {
        arr[size - rotations + 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 left: ");
    scanf("%d", &rotations);

    // Perform the left rotation
    leftRotate(arr, size, rotations);

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

    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 left: 2
Array after left 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 left.
  3. Perform the Rotation:
    • The first 'rotations' elements are stored in a temporary array.
    • The remaining elements are shifted to the left.
    • The elements in the temporary array are moved to the end 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 left.
  • Step 3: Perform the Rotation
    • The first 'rotations' elements are stored in a temporary array.
    • A loop runs to shift the rest of the elements to the left.
    • Another loop runs to copy the elements from the temporary array to the end of the original array.
  • Step 4: Print the Result
    • The program prints the elements of the rotated array.

Recommended Posts