Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Perform insert, delete, merge and delete multiple occurrences of a number from an array

Posted By: Easy Tutor     Category: C++ Programming     Views: 5931

Perform the following operations on an array:

a) insert
b) delete
c) merge
d) delete multiple occurrences of a number

Code for Perform insert, delete, merge and delete multiple occurrences of a number from an array in C++ Programming

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

int display_menu();
int array_no=1; //CURRENT ARRAY 1 OR 2class array
{
 int arr[20];
 int n;
 public:
    array()
    {
     n=0;
    }
    void add_n(int);
    int getval(int);
    int getsize();
    void getelements();
    void display();
    void insert();
    void delete_element();
    void merge_array(array);
};

int array :: getsize()
{
 return n;
}
void array :: add_n(int x)
{
 n=n+x;
}
int array :: getval(int i)
{
 return (arr[i]);
}
void array :: getelements()
{
  cout<<"Enter Number of elements you want to enter :";
  cin>>n;
  for(int i=0;i<n;i++)
  {
   cout<<"a["<<i<<"] :=>";
   cin>>arr[i];
  }
}

void array :: display()
{
  cout<<endl;
  if(n==0)
  {
   cout<<endl<<"No Element !!!"<<endl;
  }

  for(int i=0;i<n;i++)
  {
    cout<<"a["<<i<<"] :"<<arr[i]<<endl;
  }
}

void array :: insert()
{
  display();
  int pos;
  cout<<endl<<"[Enter Number Between 0 "<<"And "<<n<<endl;;
  cout<<endl<<"Enter Position Where you want to Insert :";
  cin>>pos;
  if(pos < 0 || pos >n)
  {
   cout<<"Out of Range !!!";
  }
  else
  {
  for(int i=n;i>pos;i--)
  {
   arr[i]=arr[i-1];
  }
  cout<<"Enter Number :";
  cin>>arr[pos];
  cout<<"Number Inserted at the Position "<<pos<<endl;
  n=n+1;
  }
}

void array :: delete_element()
{
  display();
  //Delete Multiple Occurance of a Number in an Arrayint no,i,pos,j;
  cout<<endl;
  cout<<"Enter Number to Delete :";
  cin>>no;

  for(i=0;i<n;i++)
  {
   pos=i;
   if(arr[i]==no)
   {
    for(j=pos;j<n-1;j++)
    {
     arr[j]=arr[j+1];
    }
    n=n-1;
    i--;
   }
  }
}

void array :: merge_array(array a1)
{
  for(int i=0;i<a1.getsize();i++)
  {
    arr[i+n]=a1.getval(i);
  }
  add_n(a1.getsize());
  cout<<"Second Array merged with current array ...";
}






void main()
{
 clrscr();
  array a1,a2;
 int arr_no;
 while(1)
 {
 switch(display_menu())
 {
  case 1: cout<<"Select Array  [ 1 Or 2 ]";
      cin>>arr_no;
      if(arr_no==1 || arr_no==2)
      {
       array_no=arr_no;
      }
      else
      {
       cout<<"Invalid Number entered !!!!";
       getch();
      }
      break;
  case 2 : if(array_no==1)
       {
        a1.getelements();
       }
       else
       {
        a2.getelements();
       }
       break;
  case 3 : if(array_no==1)
       {
         a1.insert();
       }
       else
       {
         a2.insert();
       }
       getch();
       break;
  case 4 : if(array_no==1)
       {
         a1.delete_element();
       }
       else
       {
        a2.delete_element();
       }
       getch();
       break;

  case 5 : if(array_no==1)
       {
        a1.display();
       }
       else
       {
        a2.display();
       }
       getch();
       break;









  case 6 : if(array_no==1)
       {
         a1.merge_array(a2);
       }
       else
       {
         a2.merge_array(a1);
       }
       getch();
       break;
  case 7 : exit(1);
 }
 }
}

int display_menu()
{
  int ch;
  clrscr();
  cout<<endl<<"\t\t\t[ Current Array is :"<<array_no<<"]"<<endl;
  cout<<"\t\t\t| 1 | - Select Array"<<endl;
  cout<<"\t\t\t| 2 | - Enter Elements"<<endl;
  cout<<"\t\t\t| 3 | - Insert"<<endl;
  cout<<"\t\t\t| 4 | - Delete"<<endl;
  cout<<"\t\t\t| 5 | - Display"<<endl;
  cout<<"\t\t\t| 6 | - Merge Second Array"<<endl;
  cout<<"\t\t\t| 7 | - Exit"<<endl;
  cout<<"\t\t\tEnter Your Choice :";
  cin>>ch;
  return(ch);
}
  
Share: 



Easy Tutor
Easy Tutor author of Perform insert, delete, merge and delete multiple occurrences of a number from an array is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!