Cyclic Rotate 1D Array using C++

#include<iostream>
using namespace std;
void cyclicRotate(int a[],int n)
{
int tmp,j;
tmp=a[n-1];
for(j=n-1;j>0;j--)
{
a[j]=a[j-1];
}
a[0]=tmp;
}
int main()
{
int a[5]={22, 30, 15, 18, 50},i,j,n=5,tmp;
for(i=0;i<n;i++)
{
cout<<"Step-"<<i+1<<": ";
for(j=0;j<n;j++)
{
cout<<a[j]<<"\t";
}
cout<<endl;
cyclicRotate(a,n);
}
}


No comments

Post your comments

Powered by Blogger.