-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path09_merge_arrays.c
77 lines (59 loc) · 1.9 KB
/
09_merge_arrays.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// // Write a function in C to merge two arrays of the same size sorted in descending order.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define ARRAY_SIZE_1 10
#define ARRAY_SIZE_2 5
#define ARRAY_SIZE_3 ARRAY_SIZE_1 + ARRAY_SIZE_2
// // Functions Declarations (Prototypes)
void printArray(int[], int);
void mergeArrays(int[], int, int[], int, int[]);
// // Main Function Start
int main()
{
// // Declare Array of Variable size
int arr1[ARRAY_SIZE_1] = {33, 23, 20, 16, 12, 10, 5, -3, -5, -10};
int arr2[ARRAY_SIZE_2] = {103, 103, 99, 66, 13};
int mergedArray[ARRAY_SIZE_3];
// // Print First Array
puts("\n\n>>>>>>>> Elements of First Array <<<<<<<<<");
printArray(arr1, ARRAY_SIZE_1);
// // Print Second Array
puts("\n\n>>>>>>>> Elements of Second Array <<<<<<<<<");
printArray(arr2, ARRAY_SIZE_2);
// // Merge arr1 and arr2
mergeArrays(arr1, ARRAY_SIZE_1, arr2, ARRAY_SIZE_2, mergedArray);
// // Print Array of Merged Array
puts("\n\n>>>>>>>> Elements of Merged Array <<<<<<<<<");
printArray(mergedArray, ARRAY_SIZE_3);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Functions Definitions 👇👇
// // Function to Display Array Elements
void printArray(int arr[], int size)
{
putch('\n'); // // Add new line
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
putch('\n'); // // Add new line
}
// // Function to Merge Two Arrays of Same Size Sorted in Descending order
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int mergedArray[])
{
int k = 0, i = 0, j = 0;
while (i < size1 && j < size2)
{
if (arr1[i] > arr2[j])
mergedArray[k++] = arr1[i++];
else
mergedArray[k++] = arr2[j++];
}
while (i < size1)
mergedArray[k++] = arr1[i++];
while (j < size2)
mergedArray[k++] = arr2[j++];
}