//PROGRAM TO MERGE TWO ARRAYS PROVIDED THAT ARRAYS ARE NOT ENTERED IN SORTED ORDER. A MERGED ARRAY CONTAINS ELEMENTS IN SORTED ORDER
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[10],t,i,j,k,l,n,m;
clrscr();
printf("enter the limit of first array\n");
scanf("%d",&m);
printf("enter the limit of second array\n");
scanf("%d",&n);
printf("enter the elements of array1\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
for(j=0;j<m-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("the elements of array1\n");
for(i=0;i<m;i++)
{
printf("%d\n",a[i]);
}
printf("enter the elements of array2\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(b[j]>b[j+1])
{
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
printf("the elements of array2\n");
for(i=0;i<n;i++)
{
printf("%d\n",b[i]);
}
for(i=0,j=0,k=0;i<m&&j<n;)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
k++;
}
else if(a[i]>b[j])
{
c[k]=b[j];
k++;
j++;
}
else
{
c[k]=a[i];
i++;
j++;
k++;
}
}
for(;i<m;i++)
{
c[k]=a[i];
k++;
}
for(;j<n;j++)
{
c[k]=b[j];
k++;
}
printf("elements of third array are\n");
for(i=0;i<k;i++)
printf("%d\n",c[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[10],t,i,j,k,l,n,m;
clrscr();
printf("enter the limit of first array\n");
scanf("%d",&m);
printf("enter the limit of second array\n");
scanf("%d",&n);
printf("enter the elements of array1\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
for(j=0;j<m-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("the elements of array1\n");
for(i=0;i<m;i++)
{
printf("%d\n",a[i]);
}
printf("enter the elements of array2\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(b[j]>b[j+1])
{
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
printf("the elements of array2\n");
for(i=0;i<n;i++)
{
printf("%d\n",b[i]);
}
for(i=0,j=0,k=0;i<m&&j<n;)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
k++;
}
else if(a[i]>b[j])
{
c[k]=b[j];
k++;
j++;
}
else
{
c[k]=a[i];
i++;
j++;
k++;
}
}
for(;i<m;i++)
{
c[k]=a[i];
k++;
}
for(;j<n;j++)
{
c[k]=b[j];
k++;
}
printf("elements of third array are\n");
for(i=0;i<k;i++)
printf("%d\n",c[i]);
getch();
}
No comments:
Post a Comment