You are on page 1of 2

Problem No: 19

Problem Name: Write a program in C to merge two arrays of the same size.
Problem Explanation: Declare three arrays. Take an input of N1 as the size of the
first array. Take the input for the first array. Take an input of N2 as the size of the second
array. Take the input for the values of the second array. Then Combine all values of the
first and the second array into the combined array or third array. Then print out the
combined array.
Source Code:
#include<stdio.h>
int main()
{
int a,i,c; //declare variable.
printf("first array size ");
scanf(" %d",&a); //user input of array size.
int arr1[a]; //declare array1.
printf("First array elements ");
for(i=0;i<a;i++) //user input of elements of array1.
{
scanf(" %d",&arr1[i]);
}
printf("Second array size ");
scanf(" %d",&c); //user input of array2 size.
int arr2[c]; //array2 declared.
printf("Second array elements ");
for(i=0;i<c;i++) //user input of array2 elements.
{
scanf(" %d",&arr2[i]);
}
int arr3[a+c]; //array3 size by adding previous two sizes.
for(i=0;i<a;i++)
{
arr3[i]=arr1[i]; //copying array1 to array3.
}
for(i=0;i<c;i++)
{
arr3[a+i]=arr2[i]; //copying array2 to array3.
}
for(i=0;i<a+c;i++)
{
printf("%d ",arr3[i]); //printing the merged array.
}

Screenshot:

You might also like