I am not able to run this code, please help!
#include <iostream>
using namespace std;
class Array
{
private:
int n;
int d;
int *A;
public:
Array()
{
n = 10;
d = 1;
A = new int[n];
}
Array(int sz, int rot)
{
n = sz;
d = rot;
A = new int[n];
for(int i = 0; i < n; i++)
{
cin>>A[i];
}
}
~Array()
{
delete []A;
}
int rotate();
void Insert(int n,int x);
};
int Array::rotate()
{
int z = 1;
int *B;
B = new int[n];
while(z <= d)
{
for(int i = 0; i < n; i++)
{
B[i] = A[i];
}
for(int i = 0,j = 0; i < n-1, j < n-1; i++,j++)
{
A[i] = B[j+1];
}
A[n-1] = B[0];
z++;
}
}
void Array::Insert(int index,int x)
{
int i;
if(index>=0 && index <=n)
{
for(i=n;i>index;i--)
A[i]=A[i-1];
A[index]=x;
n++;
}
}
int main()
{
Array arr1(5,2);
arr1.Insert(0,5);
arr1.Insert(1,10);
arr1.Insert(2,20);
arr1.Insert(3,30);
arr1.Insert(4,40);
cout<<arr1.rotate();
return 0;
}
The question is as follows:
A left rotation operation on an array of size n shifts each of the array's elements l unit to the left. Given an integer, d, rotate the array that many steps left and return the result.
Ex:
d = 2
arr = [1,2,3,4,5]after 2 rotations:
arr1 = [3,4,5,1,2]Input Format:
The first line contains two space-separated integers that denote n, the number of integers, and ,d the number of left rotations to perform.The second line contains n space-separated integers that describe.