Write an array function to shift all elements by one to the left and move the first element into the last position. For example, 2 6 10 15 26 would be transformed into 6 10 15 26 2.
#include<iostream>
using namespace std;
int main()
{
int n,temp,i;
cout<<"Enter the number of elements in the array : ";
cin>>n;
int arr[n];
cout<<"Enter the elements in the array : ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
temp=arr[0];
for(i=0;i<n;i++)
{
arr[i]=arr[i+1];
if((i+1)>(n-1))
{
arr[i]=temp;
}
}
cout<<"Elements of array shifted by one to the left : \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
Comments
Leave a comment