Thursday 26 June 2014

C++ program to split an array at the given position and move the second part to the front


Here is a C++program to split an array at the given position and move the second part to the front


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    int a[20],firt[20],num,n;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements : ";
    for(int i=0;i<num;i++)
       cin>>a[i];
    cout<<"Enter value of n : ";
    cin>>n;
    for(int i=0;i<n;i++)
        firt[i]=a[i];
    for(int i=0;i<num-n;i++)
        a[i]=a[i+n];
    for(int i=0;i<n;i++)
        a[i+num-n]=firt[i];
    cout<<"After shifting ";
    for(int i=0;i<num;i++)
        cout<<a[i]<<" ";
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment