Thursday, 6 December 2012

Append two arrays cpp program


Program to append two integer arrays into a single array

 

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

class array
{private:
          int a1[100],a2[100],a3[100];
          int n1,n2;
 public:
          void readarray();
          void displayarray();
          void append();
};
void array::readarray()
{
          cout<<"\n    Enter the elements of the first array";
cout<<"\n    How many elements in the first array ";
cin>>n1;
for(int i=0;i<n1;i++)
                   {cout<<"\n  Enter the element " <<i+1 <<" ";
cin>>a1[i];
}
cout<<"\n    Enter the elements of the second array";
cout<<"\n    How many elements in the second array ";
cin>>n2;
for( i=0;i<n2;i++)
                   {cout<<"\n  Enter the element " <<i+1 <<" ";
cin>>a2[i];
}
}

void array::displayarray()
{        cout<<"\n   Elements of the first array\n";
for(int i=0;i<n1;i++)
cout<<a1[i] <<" ";
cout<<"\n    Elements of the second array\n";
for(i=0;i<n2;i++)
cout<<a2[i] <<" ";
}

void array::append()
{        for(int i=0;i<n1;i++)
                   a3[i]=a1[i];
for(int j=0;j<n1+n2;i++,j++)
                   a3[i]=a2[j];
cout<<"\n    Elements of the appended array\n";
for( i=0;i<n1+n2;i++)
cout<<a3[i] <<" ";
}
void main()
{ int c;
  char ch;
   array a;
 while(1)
  {      clrscr();
          cout<<"Perform the following operations "<<endl;
          cout<<"------------------------------------------"<<endl;
          cout<<"1.Insert elements to array \n";
          cout<<"2.Display arrays \n";
          cout<<"3.Append arrays \n";
          cout<<"4.Exit from Program \n";
          do
          {        cout<<"Select your choice(1:3)";
                   cin>>c;
          }
          while((c<1)||(c>4));
   if(c==4)exit(1);
   clrscr();
   switch(c)
     {   case 1:
                              a.readarray();
break;
          case 2:
                   a.displayarray();
                    break;
          case 3:
                   //a.displayarray();
                   a.append();
                   break;
     }
  cout<<endl<<"Press any key to continue...";
  getch();
   }
}

No comments:

Post a Comment