PROGRAM TO MERGE TWO ARRAYS

/*PROGRAM TO MERGE TWO ARRAYS*/
#include<conio.h>
#include<iostream.h>
#define MAX1 10
#define MAX2 5
void main()
{
int n1=0,n2=0,a[MAX1],b[MAX2],num,i,j;
char ch;
clrscr();
cout<<“Enter elements in the first array: “<<endl;
do
{
cout<<“Enter element: “;
cin>>num;
a[n1]=num;
n1++;
if(n1>=MAX1)
{
cout<<“!!Limit exceeded!!”;
break;
}
cout<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<endl<<“After insertion elements of first array are: “<<endl;
for(i=0;i<n1;i++)
{
cout<<a[i]<<“\t”;
}
cout<<endl<<“Inserting elements in the second array:”<<endl;
do
{
cout<<“Enter the element: “;
cin>>num;
b[n2]=num;
n2++;
if(n2>=MAX2)
{
cout<<“!!Limit exceeded!!”;
break;
}
cout<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<“Elements of second array are:”<<endl;
for(i=0;i<n2;i++)
{
cout<<b[i]<<“\t”;
}
if(n1+n2>MAX1)
cout<<endl<<“Size of first array is not sufficient for merging”<<endl;
else
{
j=0;
for(i=n1;i<n1+n2;i++)
{
if(j<n2)
{
a[i]=b[j];
j++;
}
}
cout<<endl<<“After merging elements of first array are:”<<endl;
for(i=0;i<n1+n2;i++)
{
cout<<a[i]<<“\t”;
}
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *