Friday, 5 July 2013

Write a program to show the concept of merging of tow array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],i,r,s,m;
int na=1,nb=1,nc=1;
printf("enter no. of array element of A\n");
scanf("%d",&r);
printf("enter array elements of A\n");
for(i=1;i<=r;i++);
{
scanf("%d",&a[i]);
}

printf("enter no. of array element of B\n");
scanf("%d",&s);
printf("enter array elements of B\n");
for(i=1;i<=s;i++)
{
scanf("%d",&b[i]);
}

while(na<=r&&nb<=s)
{
if(a[na]<b[nb])
{
c[nc]=a[na];
nc=nc+1;
nb=nb+1;
}
else
{
    c[nc]=b[nb];
    nc=nc+1;
    nb=nb+1;
}
}
if(na>r)
for(i=nb;i<=s;i++)
{
c[nc]=b[i];
nc=nc+1;
}

else
{
for(i=na;i<=r;i++)
{
c[nc]=a[i];
nc=nc+1;
}
printf("merged array is\n");
for(i=1;i<=s+r;i++)
{
printf("%d\n",c[i]);
}
}
}

No comments:

Post a Comment