-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeArrays.c
55 lines (55 loc) · 1.12 KB
/
mergeArrays.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
#include<stdio.h>
void read(int [],int);
void display(int [],int);
void merge(int [], int [], int [], int, int);
void main()
{
int m, n, a[10], b[10], c[20];
printf("Enter number of elements for array A: ");
scanf("%d", &m);
printf("Enter number of elements for array B: ");
scanf("%d", &n);
printf("Enter elements for A in ascending order: ");
read(a, m);
printf("Enter elements for B in ascending order: ");
read(b, n);
printf("The elements in array A are:");
display(a, m);
printf("The elements in array B are:");
display(b, n);
merge(c, a, b, m, n);
printf("The merged elements in array C are: ");
display(c, m + n);
}
void read(int x[10], int n)
{
int i;
for(i = 0; i < n; i++)
scanf("%d", &x[i]);
}
void display(int x[10], int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ", x[i]);
printf("\n");
}
void merge(int z[20], int x[10], int y[10], int m, int n)
{
int i = 0, j = 0, k = 0;
while(i < m && j < n)
{
if(x[i] < y[j])
z[k++] = x[i++];
else
z[k++] = y[j++];
}
while(j < n)
{
z[k++] = y[j++];
}
while(i < m)
{
z[k++] = x[i++];
}
}