Thursday, February 2, 2017

Merge Array


Read two one dimensional arrays of five elements. Merge both the arrays into third array and display the same.


#include<stdio.h>
int main()
{
int i,count=0, arr1[5], arr2[5], arr3[10];
//Read array1..
for(i=0; i<5; i++)
{
printf("Enter arr1[%d]: ",i);
scanf("%d",&arr1[i]);
}
printf("\n");
//Read array2..
for(i=0; i<5; i++)
{
printf("Enter arr2[%d]: ",i);
scanf("%d",&arr2[i]);
}
//Copy arr1 into arr3...
for(i=0; i<5; i++)
{
arr3[i] = arr1[i];
}
//Append arr2 into arr1..
for(i=0;i<5;i++)
{
arr3[i+5]=arr2[i];
}
printf("Elements in Array 3 are :\n");
for(i=0;i<10;i++)
{
printf("Arr3[%d] = %d \n", i, arr3[i]);
}
}

Output of the program:

Enter arr1[0]: 1
Enter arr1[1]: 2
Enter arr1[2]: 3
Enter arr1[3]: 4
Enter arr1[4]: 5

Enter arr2[0]: 6
Enter arr2[1]: 7
Enter arr2[2]: 8
Enter arr2[3]: 9
Enter arr2[4]: 10
Elements in Array 3 are :
Arr3[0] = 1
Arr3[1] = 2
Arr3[2] = 3
Arr3[3] = 4
Arr3[4] = 5
Arr3[5] = 6
Arr3[6] = 7
Arr3[7] = 8
Arr3[8] = 9
Arr3[9] = 10

* * * * *

No comments:

Post a Comment