Edit: Solved
This is a program to rotate an array of size 'n' by 'd' towards the left. For example: 1 2 3 4 5 6 7 for d=3 goes to 4 5 6 7 1 2 3. My problem is regarding the final loop in int main() that is being used to print the array. That loop is not printing the array upto all it's 'n' members.
I've tried printing the array for a specific 'n' for example n=7. It works in that case. Hence, I don't think there is any logical error in the rotleft() function.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void rotleft(int arr[],int d,int n)
{
int temp[d];
for(int i=0;i<d;i++)
{
temp[i]=arr[i];
}
for(int i=0;i<n-d;i++)
{
arr[i]=arr[i+d];
}
for(int i=n-d;i<n;i++)
{
arr[i]=temp[i-n+d];
}
}
int main()
{
int n,d,arr[n];
cin>>n>>d;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
rotleft(arr,d,n);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
When I replaced 'n' by 7 in the final loop I got the accurate result, but for general 'n', it shows 4 5 6 instead of 4 5 6 7 1 2 3 (refer to above example).