C Program to right rotate an array – In this article, we will detail in on the various means of how to right rotate an array in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
- Using Standard Method
- Using Function
An array is a sequential collection of elements in a horizontal fashion. The elements in an array are defined by the user themselves.
As you can see in the image uploaded above, firstly, you need to enter the size of the array.
The size of the current array mentioned is taken to be 5.
Later, enter the elements into the array.
The elements entered in this are as follows:
1 2 3 4 5
Then, mention the number of times to right rotate.
In this pic, the number is 4.
So, the final array would be like:
2 3 4 5 1
Thus, the multiple methods to do so in C programming are as follows:
Using Standard Method
- Read the array size and store the size into the variable n.
2) Read the entered elements and store the elements in the array a[] as scanf(“%d”,&a[i]) using for loop.
3) Read the k value which represents how many times right rotate the array.
4) Right, rotate the array up to k times as follows
for loop iterates from i=0 to i<k
a) Store the last element into the variable temp.
b) Inner for loop iterates from j=n-1 to j>0
move the a[j-1] to a[j],repeat until j>0.
c) Initialize the temp value to a[0].
Repeat these three steps until i<k.
5) After completion of the 4th step, we will get the right rotated array.
6) In the given example n=5 and elements are 1,2,3,4,5 and k=4
1st right rotate i=0
temp=5 and j=4 then a[4]=4,a[3]=3,a[2]=2,a[1]=1 and a[0]=5.Then the array is 5,1,2,3,4.
2nd right rotate i=1
temp=4 and j=4 then a[4]=3,a[3]=2,a[2]=1,a[1]=5 and a[0]=4.Then the array is 4,5,1,2,3.
3rd right rotate i=2
temp=3 and j=4 then a[4]=2,a[3]=1,a[4]=5,a[5]=4 and a[0]=3.Then the array is 3,4,5,1,2.
4th right rotate i=3
temp=2 and j=4 then a[4]=1,a[3]=5,a[2]=4,a[1]=3 and a[0]=2.Then the array is 2,3,4,5,1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <conio.h> int main() { int a[10000],i,n,j,k,temp; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("how many times right rotate : "); scanf("%d", &k); for(i=0; i<k; i++) { temp=a[n-1]; for(j=n-1; j>0; j--) { a[j]=a[j-1]; } a[j]=temp; } printf("\narray elements after right rotate : "); for(i=0; i<n; i++) { printf("%d ",a[i]); } } |
Output:
1 2 3 4 5 |
Enter size of the array: 5 Enter elements in array : 1 2 3 4 5 how many times right rotate: 4 array elements after right rotate : 2 3 4 5 1 |
Using Function
- The main() function calls the rightrotate() function by passing array,size of the array,the k value as arguments.
2) The function rightrotate() rotates the given array k times as follows,
for loop iterates from i=0 to i<k.k represents how many times right rotates the array.
a) Initialize the last element to temp.
b) Inner for loop iterates from j=n-1 to j>0
Move the last element to first. Repeat this step until j>0.
c) At j=0,initialize a[j]=temp.
Repeat these three steps until i<k.
3) The main() function calls the print() to print the elements of the array which is right rotated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <conio.h> int rightrotate(int *a,int n,int k) { int i,j,temp; for(i=0; i<k; i++) { temp=a[n-1]; for(j=n-1; j>0; j--) { a[j]=a[j-1]; } a[j]=temp; } } print(int *a,int n) { int i; for(i=0; i<n; i++) { printf("%d ",a[i]); } } int main() { int a[10000],i,n,j,k,temp; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("how many times right rotate : "); scanf("%d", &k); rightrotate(a,n,k); printf("\narray elements after right rotate : "); print(a,n); } |
1 2 3 4 5 |
Enter size of the array: 5 Enter elements in array : 1 2 3 4 5 how many times right rotate : 3 array elements after right rotate : 3 4 5 1 2 |