Given two ordered arrays of integers, write a program to merge the two-arrays to get an ordered array.




#include<iostream.h>
#include<conio.h>
class ordered
{
int *a;
int n;
public:
ordered();
ordered(int n1);
void accept();
void print();
ordered merge(ordered obj1);
};
ordered:: ordered()
{
cout<<"enter no of elements of ordered array"<<endl;
cin>>n;
a=new int[n]   ;

}
ordered::ordered(int n1)
{

  n=n1;
  a=new int[n]  ;

  }

void ordered::accept()
{
cout<<"enter the ordered array"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
ordered ordered::merge(ordered obj1)
{
int count=0;
    ordered h(obj1.n+n);
    for(int i=0;i<n;i++)
    {
    h.a[count++]=a[i];
    }
    for(i=0;i<obj1.n;i++)
    {
    h.a[count++]=obj1.a[i];
    }

     for(i=0;i<h.n;i++)
     {
     for(int j=i+1;j<h.n;j++)
     {
     if(h.a[i]>h.a[j])
     {
     int temp=h.a[i];
     h.a[i]=h.a[j];
     h.a[j]=temp;
     }
     }
     }


     return(h);

     }
     void ordered::print()
     {
     for(int i=0;i<n;i++)
     {
      cout<<a[i]<<endl;
      }
      }

     void main()
     {
     clrscr();
     ordered obj;
     obj.accept();
     ordered obj2;
     obj2.accept();
     cout<<"the first ordered array is"<<endl;
     obj.print();
     cout<<"the second ordered array is "<<endl;
     obj2.print();
     obj =obj.merge(obj2);
     cout<<"the merged array is "<<endl;
     obj.print();
     getch();

}


Output:- 

Comments