Friday, November 23, 2012

C++: Shifting array

 //This C++ program makes shift to all array members.

#include<iostream.h>
#include <cstdlib> // included to use exit(0)
void main()
{
     int a[10];int x,y;

     cout<<"Enter 10 numbers\n";
     ;
     for(int i=0;i<10;i++)
     {
         cout<<"No."<<i<<endl;
         cin>>x;
         a[i]=x;
     }
     cout<<"\n[";
    for(int k=0;k<10;k++)
        cout<<a[k]<<"  ";
    cout<<"]";
    cout<<"\n Press 1 to shift right.\n Press 2 to shift left.\n press 0 to exit.\n";
        cin>>y;
        if(y==1)
            {
     
            for(int j=9;j>0;j--)
                {
                   
                a[j]=a[j-1];
           
                }
                a[0]=0; //empty

                cout<<"\n[";
                for(int r=0;r<10;r++)
                cout<<a[r]<<"  ";
                cout<<"]"<<endl;       
            }
        else if (y==2)
            {
       
            for(int j=0;j<9;j++)
                {
               
                a[j]=a[j+1];

                }

                a[9]=0; //empty

                cout<<"\n[";
                for(int r=0;r<10;r++)
                cout<<a[r]<<"  ";
                cout<<"]"<<endl;

            }
        else if (y==0)
            exit(0);
       
            else
               cout<<"Go To HeLL";



};

No comments:

Post a Comment