Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One-D Arrays and functions: Complete the following program. You will mainly impl

ID: 3528241 • Letter: O

Question

One-D Arrays and functions: Complete the following program. You will mainly implement the MERGE function, and a PRINT_ARRAY function! main() { /* 1. Declare three integer arrays as follows */ int a[50], b[70], c[120]; /* 2. implement a function set_array_rand(int x[], int n) and call it to generate the values in array a and b randomly. */ set_array_rand(a, 50); set_array_rand(b, 70); /* 3. using the selection_sort(double x[], int n) function we implemented in class, sort the elements in a and b arrays. */ selection_sort(a, 50); selection_sort(b, 70); /* 4. implement a MERGE function and call it as follows to merge the values in arrays a and b into array c such that the values in c will be sorted after merging */ MERGE(a, 50, b, 70, c, 120); /* 5. print the values in array c */ PRINT_ARRAY(

Explanation / Answer

/* YOUR CODE */


void MERGE(int a[], int na, int b[], int nb, int c[], int nc) {
int i, j, k;

j = k = 0;

for (i = 0; i < na + nb;) {
if (j < na && k < nb) {
if (a[j] < b[k]) {
c[i] = a[j];
j++;
}
else {
c[i] = b[k];
k++;
}
i++;
}
else if (j == na) {
for (; i < na + nb;) {
c[i] = b[k];
k++;
i++;
}
}
else {
for (; i < na + nb;) {
c[i] = a[j];
j++;
i++;
}
}
}
}