PROGRAM TO MERGE TWO ARRAYS

/*PROGRAM TO MERGE TWO ARRAYS*/
#include<conio.h>
#include<stdio.h>
#define MAX1 10
#define MAX2 5
void main()
{
int n1=0,n2=0,a[MAX1],b[MAX2],num,i,j;
char ch;
clrscr();
printf(“Enter elements in the first array:  \n”);
do
{
printf(“Enter element: “);
scanf(“%d”,&num);
a[n1]=num;
n1++;
if(n1>=MAX1)
{
printf(“!!Limit exceeded!!”);
break;
}
printf(“Do you want to continue?? press y for yes otherwise press n\t”);
fflush(stdin);
scanf(“%c”,&ch);
}while(ch==’y’||ch==’Y’);
printf(“\nAfter insertion elements of first array are: \n”);
for(i=0;i<n1;i++)
{
printf(“\t%d”,a[i]);
}
printf(“\nInserting elements in the second array:\n”);
do
{
printf(“Enter the element: “);
scanf(“%d”,&num);
b[n2]=num;
n2++;
if(n2>=MAX2)
{
printf(“!!Limit exceeded!!”);
break;
}
printf(“Do you want to continue?? press y for yes otherwise press n\t”);
fflush(stdin);
scanf(“%c”,&ch);
}while(ch==’y’||ch==’Y’);
printf(“Elements of second array are:\n”);
for(i=0;i<n2;i++)
{
printf(“\t%d”,b[i]);
}
if(n1+n2>MAX1)
printf(“\nSize of first array is not sufficient for merging\n”);
else
{
j=0;
for(i=n1;i<n1+n2;i++)
{
if(j<n2)
{
a[i]=b[j];
j++;
}
}
printf(“After merging elements of first array are:\n”);
for(i=0;i<n1+n2;i++)
{
printf(“\t%d”,a[i]);
}
}
getch();
}

Leave a Reply

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