Answer to Question #198548 in C++ for shuvo

Question #198548

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.


1
Expert's answer
2021-05-26T10:38:37-0400
#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]<<" ";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment