Write a c program to make a circular rotation of an array by k positions using the loops
ADVERTISEMENTS
Write a c program to make a circular rotation of an array by k positions using the loops. In this program, you will learn how to make a circular rotation of an array by k positions using the loops.
Take an example to make this circular rotation through a c program:
// C Program to Make a Circular Rotation of an Array by K Positions
#include <stdio.h>
#define N 100
int main() {
int e, r, d;
int arru[N];
// e is the number of array elements
// r is the number of rotations of the array
// d is the number of indexes to display
// arru is the array container
printf("-----Enter the number of elements of the array-----\n");
scanf("%d", &e);
printf("\n-----Enter the number of rotations of the array-----\n");
scanf("%d", &r);
printf("\n-----Enter the number of indexes to be displayed-----\n");
scanf("%d", &d);
printf("\n-----Input the array elements-----\n");
for (int i = 0; i < e; i++) {
scanf("%d", &arru[i]);
}
printf("\n\n------The Array Elements are------\n");
for (int i = 0; i < e; i++) {
printf("%d ", arru[i]);
}
printf("\n");
// Remove the number of full array rotations from k
r %= e;
for (int i = 0; i < d; i++) {
int in;
printf("\n------Enter the index of the array to be displayed------\n");
scanf("%d", &in);
printf("\n------The element in the array is------\n");
// Calculate the new index
printf("%d\n", arru[(e + in - r) % e]);
}
return 0;
}
Output
-----Enter the number of elements of the array-----
4
-----Enter the number of rotations of the array-----
2
-----Enter the number of indexes to be displayed-----
2
-----Input the array elements-----
12 34 56 43
------The Array Elements are------
12 34 56 43
------Enter the index of the array to be displayed------
2
------The element in the array is------
12
------Enter the index of the array to be displayed------
3
------The element in the array is------
34