Program to Merge Two Arrays
Algorithm
1.Read the size of array A and assign it to m
2.Print "Enter elements of array A"
3.for(i=0;i<m;i++)
1.Read values a[i]
4.Read the size of array B and assign it to n
5.Print "Enter elements of array B"6.for(i=0;i<n;i++)
1.Read values b[i]
7.for(i=0;i<m;i++)
1.Assign c[i]=a[i]
8.for(j=m;j<m+n;j++)
1.Assign c[j]=b[k]
2.k=k+1
9.Print "After merging the array is : "
10.for(i=0;i<m+n;i++)
1.print c[i]
11.Stop
Program
#include <stdio.h>int main()
{
int a[100], b[100], c[100], m, n,i,j,k=0;
printf(" Enter size of array A: ");
scanf("%d", &m);
printf(" Enter elements of array A: ");
for(i=0; i<m; i++) //For assigning value to array A
scanf("%d", &a[i]);
printf(" Enter size of array B: ");
scanf("%d", &n);
printf(" Enter elements of array B: ");
for(i=0; i<n; i++) //For assigning value to array B
scanf("%d", &b[i]);
for(i=0; i<m; i++)
c[i] = a[i];
for(j=m; j<m+n; j++)
{
c[j] = b[k];
k = k+1;
}
printf(" After merging the new array is: ");
for(i=0; i<m+n; i++)
printf(" %d", c[i]);
return 0;
}
Output :
Enter size of array A: 4
Enter elements of array A: 1 2 3 4
Enter size of array B: 4
Enter elements of array B: 5 6 7 8
After merging the new array is : 1 2 3 4 5 6 7 8
No comments