mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Rotate elements of array to right
you can rotate elements of array to the right
This commit is contained in:
parent
8a3d988979
commit
cfbaec02ad
31
Array_right_rotation.cpp
Normal file
31
Array_right_rotation.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
int main(){
|
||||||
|
int n,k;
|
||||||
|
cout<<"Enter size of array=\t";
|
||||||
|
cin>>n;
|
||||||
|
cout<<"Enter Number of indices u want to rotate the array to right=\t";
|
||||||
|
cin>>k;
|
||||||
|
int a[n];
|
||||||
|
cout<<"Enter elements of array=\t";
|
||||||
|
for(int i=0;i<n;i++)
|
||||||
|
cin>>a[i];
|
||||||
|
int temp=0;
|
||||||
|
for(int i=0;i<k;i++){
|
||||||
|
temp=a[n-1];
|
||||||
|
for(int j=n-1;j>=0;j--){
|
||||||
|
if(j==0){
|
||||||
|
a[j]=temp;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
a[j]=a[j-1];}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
cout<<"Your rotated array is=\t";
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
cout<<a[i]<<" ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user