I'm solving a problem on array rotation. We take an array and the number of transitions to take place as input from the user. For example, Assume that the array A[]={1,2,3,4,5} and the number of transitions(d)=2. Then, the output will be : {3,4,5,1,2} , etc.
This is the code that I've written, but I'm getting a Segmentation fault. What should I do?
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,d;
cout<<"Enter the number of elements:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the number of transitions:"<<endl;
cin>>d;
int temp[d];
//To store the first d elements of the array
for(int i=0;i<d;i++)
{
temp[i]=a[i];
}
//To move the elements by d positions
for(int i=0;i<(n-d);i++)
{
a[i]=a[i+d];
}
//Fill the d elements to the end
for(int i=(n-d);i<n;i++)
{
for(int i=0;i<d;i++)
{
cin>>temp[i];
}
}
//Print the final array
cout<<"The final array is: "<<endl;
for(int i=0;i<n;i++)
{
cout<<setw(3)<<a[i];
}
return 0;
}