Write a program to merge two arrays to the third array
- #include<stdio.h>
- void main() {
- int arr1[30], arr2[30], arr3[60];
- int i, j, k, n1, n2;
- printf("Enter number of elements in 1st array : ");
- scanf("%d", &n1);
- for (i = 0; i < n1; i++) {
- scanf("%d", &arr1[i]);
- }
- printf("\nEnter number of elements in 2nd array : ");
- scanf("%d", &n2);
- for (i = 0; i < n2; i++) {
- scanf("%d", &arr2[i]);
- }
- i = 0;
- j = 0;
- k = 0;
- while (i < n1 && j < n2) {
- if (arr1[i] <= arr2[j]) {
- arr3[k] = arr1[i];
- i++;
- k++;
- }
- else {
- arr3[k] = arr2[j];
- k++;
- j++;
- }
- }
- while (i < n1) {
- arr3[k] = arr1[i];
- i++;
- k++;
- }
- while (j < n2) {
- arr3[k] = arr2[j];
- k++;
- j++;
- }
- printf("\nMerged two arrays into 3rd array is : ");
- for (i = 0; i < n1+n2; i++)
- printf("%d ", arr3[i]);
- }
একটি মন্তব্য পোস্ট করুন