Introduction
Array rotation involves shifting the elements of an array either to the left or to the right by a specified number of positions. This guide will show you how to write a C program to rotate an array to the left and to the right.
Problem Statement
Create a C program that:
- Takes the size of the array as input from the user.
- Takes the elements of the array as input.
- Takes the number of positions to rotate the array.
- Rotates the array to the left and displays the result.
- Rotates the array to the right and displays the result.
Example:
- Input: Array = [1, 2, 3, 4, 5], Rotate by 2 positions
- Output: Left Rotation = [3, 4, 5, 1, 2], Right Rotation = [4, 5, 1, 2, 3]
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
to include the standard input-output library, which is necessary for usingprintf
andscanf
functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the array size, the array elements, and the number of positions to rotate.
- Input the Array Size: Use
scanf
to take input from the user for the size of the array. - Input the Array Elements: Use a loop to take input from the user for the elements of the array.
- Input the Number of Positions to Rotate: Use
scanf
to take input from the user for the number of positions to rotate. - Rotate the Array to the Left: Use a function or loop to perform the left rotation and display the result.
- Rotate the Array to the Right: Use a function or loop to perform the right rotation and display the result.
C Program
#include <stdio.h> /** * C Program to Rotate an Array Left and Right * Author: https://www.javaguides.net/ */ // Function to rotate the array to the left by d positions void rotateLeft(int arr[], int n, int d) { int temp[d]; for (int i = 0; i < d; i++) { temp[i] = arr[i]; } for (int i = 0; i < n - d; i++) { arr[i] = arr[i + d]; } for (int i = 0; i < d; i++) { arr[n - d + i] = temp[i]; } } // Function to rotate the array to the right by d positions void rotateRight(int arr[], int n, int d) { int temp[d]; for (int i = 0; i < d; i++) { temp[i] = arr[n - d + i]; } for (int i = n - 1; i >= d; i--) { arr[i] = arr[i - d]; } for (int i = 0; i < d; i++) { arr[i] = temp[i]; } } // Function to print the array void printArray(int arr[], int n) { for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { // Step 1: Declare variables to hold the array size, elements, and rotation positions int n, d; // Step 2: Prompt the user to enter the size of the array printf("Enter the number of elements in the array: "); scanf("%d", &n); // Step 3: Declare an array to hold the elements int arr[n]; // Step 4: Input the array elements printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Step 5: Prompt the user to enter the number of positions to rotate printf("Enter the number of positions to rotate: "); scanf("%d", &d); // Step 6: Rotate the array to the left rotateLeft(arr, n, d % n); printf("Array after left rotation: "); printArray(arr, n); // Step 7: Rotate the array to the right rotateRight(arr, n, d % n); printf("Array after right rotation: "); printArray(arr, n); return 0; // Step 8: Return 0 to indicate successful execution }
Explanation
Step 1: Declare Variables
- The variable
n
is declared to store the size of the array. The variabled
is used to store the number of positions to rotate the array.
Step 2: Input the Array Size
- The program prompts the user to enter the size of the array using
printf
. Thescanf
function reads the input and stores it in the variablen
.
Step 3: Declare the Array
- The program declares an array
arr
of sizen
to hold the elements provided by the user.
Step 4: Input the Array Elements
- The program uses a
for
loop to take input for each element of the array. The loop iterates from 0 ton-1
, reading the elements usingscanf
.
Step 5: Input the Number of Positions to Rotate
- The program prompts the user to enter the number of positions to rotate the array using
printf
. Thescanf
function reads the input and stores it in the variabled
.
Step 6: Rotate the Array to the Left
- The function
rotateLeft
is used to rotate the array to the left byd
positions:- The first
d
elements are temporarily stored in an arraytemp
. - The remaining elements are shifted left by
d
positions. - The elements in
temp
are then placed at the end of the array.
- The first
Step 7: Rotate the Array to the Right
- The function
rotateRight
is used to rotate the array to the right byd
positions:- The last
d
elements are temporarily stored in an arraytemp
. - The remaining elements are shifted right by
d
positions. - The elements in
temp
are then placed at the beginning of the array.
- The last
Step 8: Display the Array
- The function
printArray
is used to print the elements of the array after the rotation.
Step 9: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example:
Enter the number of elements in the array: 5 Enter 5 elements: 1 2 3 4 5 Enter the number of positions to rotate: 2 Array after left rotation: 3 4 5 1 2 Array after right rotation: 1 2 3 4 5
Example (Right Rotation):
Enter the number of elements in the array: 4 Enter 4 elements: 10 20 30 40 Enter the number of positions to rotate: 1 Array after left rotation: 20 30 40 10 Array after right rotation: 40 10 20 30
Conclusion
This C program demonstrates how to rotate an array to the left and right by a specified number of positions. It covers basic concepts such as arrays, loops, and array manipulation, making it a useful example for beginners learning C programming.