-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path44.c
58 lines (58 loc) · 1.18 KB
/
44.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//Merging two sorted arrays
#include <stdio.h>
int main(void)
{
int i, n, j, k;
printf("Enter the size of the first array: ");
scanf("%d", &n);
int arr1[n];
printf("Enter the elements of the first array: \n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr1[i]);
}
printf("Enter the size of the second array: ");
scanf("%d", &k);
int arr2[k];
printf("Enter the elements of the second array: \n");
for (j = 0; j < k; j++)
{
scanf("%d", &arr2[j]);
}
int arr3[n + k];
i = j = 0;
int in;
for (in = 0; in < n + k; in ++)
{
if (i < n && j < k)
{
if (arr1[i] < arr2[j])
{
arr3[in] = arr1[i];
i++;
}
else
{
arr3[in] = arr2[j];
j++;
}
}
else if (i < n)
{
arr3[in] = arr1[i];
i++;
}
else
{
arr3[in] = arr2[j];
j++;
}
}
printf("The merged array is: \n");
for (in = 0; in < n + k; in++)
{
printf("%d ", arr3[in]);
}
printf("\n");
return 0;
}