Output not coming in left rotation of array

#include
using namespace std;
void lr(int *arr,int n)
{
int temp=arr[0],i;
for(int i=0;i<n-1;i++)
{
arr[i]=arr[i+1];
}
arr[i]=temp;

}
void print(int *arr,int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}

int main()
{
int n,i;
cin>>n;
int *arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=i+1;
}
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" "<<endl;
}
cout<<“after rotation”<<endl;
lr(arr,n);
print(arr,n);

}

Hello @supratik260699,

There is a problem inside lr() function:
The variable ‘i’ used inside for loop has scope only inside the loop.
The variable ‘i’ you are using in the following statement is the variable declared inside the function.
arr[i]=temp;

I have corrected your code:

Hope, this would help.
Give a like if you are satisfied.

1 Like