Home

A program to merge two single dimensional arrays into another array

C Programming Language 053

The following code shows how to write a program to merge two single dimensional arrays into another array.

Program

#include <stdio.h>

void main()
{
    int a[20], b[20], c[20], m, n, i;

    printf("Enter the size of a: ");
    scanf("%d", &m);

    printf("Enter %d elements for a:\n", m);

    for (i = 0; i < m; i++)
    {
        scanf("%d", &a[i]);
    }

    printf("Enter the size of b: ");
    scanf("%d", &n);

    printf("Enter %d elements for b:\n", n);

    for (i = 0; i < n; i++)
    {
        scanf("%d", &b[i]);
    }

    for (i = 0; i < m; i++)
    {
        c[i] = a[i];
    }
    for (i = 0; i < n; i++)
    {
        c[m + i] = b[i];
    }

    printf("Concatenation completed.\n");
    printf("Elements of c:\n");

    for (i = 0; i < m + n; i++)
    {
        printf("%d\t", c[i]);
    }

    printf("\n");
}

Result

Enter the size of a: 4
Enter 4 elements for a:
1
2
3
4
Enter the size of b: 2
Enter 2 elements for b:
5
6
Concatenation completed.
Elements of c:
1       2       3       4       5       6


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments