C Program to Merge Two Arrays


Following C program ask to the user to enter array 1 and 2 size, then ask to enter array 1 and 2 elements, to merge or add to form new array, then display the result of the added array or merged array (Here we display the direct merged array). You can also sort the two array then merge or sort after merge. So to learn about sorting, here are the techniques to sort arrays:
#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int array1[50], array2[50], size1, size2, size, i, j, k, merge[100];
    printf("Enter Array 1 Size : ");
    scanf("%d",&size1);
    printf("Enter Array 1 Elements : ");
    for(i=0; i<size1; i++)
    {
        scanf("%d",&array1[i]);
    }
    printf("Enter Array 2 Size : ");
    scanf("%d",&size2);
    printf("Enter Array 2 Elements : ");
    for(i=0; i<size2; i++)
    {
        scanf("%d",&array2[i]);
    }
    for(i=0; i<size1; i++)
    {
        merge[i]=array1[i];
    }
    size=size1+size2;
    for(i=0, k=size1; k<size && i<size2; i++, k++)
    {
        merge[k]=array2[i];
    }
    printf("Now the new array after merging is :\n");
    for(i=0; i<size; i++)
    {
        printf("%d  ",merge[i]);
    }
    getch();
}

Run:


No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...