A program to declare a global array of 10 elements, which does the following operations according to the choice of the user:
a) Inserts an element at the beginning of the array.
b) Inserts an element at the end of the array.
c) Inserts an element at the desired position in the array.
The program has three different functions begin(), end() and pos() for doing the various functions and the array elements are entered in the main program.
NOTE: The program continues till the user wants.

 

#include<iostream.h>
#include<conio.h>

int a[10];

void begin (int x)
{
a[0]=x;
}

void end(int y)
{
a[9]=y;
}

void pos(int z)
{
int n;
cout<<"Enter the desired position: ";
cin >> n;
a[n-1]=z;
}

void main()
{
 clrscr();

 int ch,i;

 do
  {
   cout<<"1.Insert an element at the begining."<<endl;
   cout<<"2.Insert an element at the end."<<endl;
   cout<<"3.Insert an element at the desired position."<<endl;
   cout<<"4.Display array elements."<<endl;
   cout<<"5.Exit."<<endl;
   cout<<"Enter your choice: ";
   cin>>ch;

   switch(ch)
    {
     case 1: {
                 cout<<"\nEnter value of element: ";
                 cin>>i;
                 begin(i);
                 break;
                }
     case 2: {
                 cout<<"\nEnter value of element: ";
                 cin>>i;
                 end(i);
                 break;
                }
     case 3: {
                 cout<<"\nEnter value of element: ";
                 cin>>i;
                 pos(i);
                 break;
                }
     case 4: {
                 clrscr();
                 for(i=0;i<=9;i++)
                  {
                    cout<<"Element at "<<i+1<<" position: "<<a[i]<<endl;
                  }
                }
     case 5: {
                 break;
                }
    default: {
                 cout<<"\nWrong choice entered.\n";
                }
    }
   cout<<endl;
  }
 while(ch!=5);
}

 

< Back