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

2) Write a function that will merge the contents oftwo sorted (ascending order)

ID: 3782789 • Letter: 2

Question

2) Write a function that will merge the contents oftwo sorted (ascending order) amays of the type DOUBLE values, use an insertion sort to sort two arrays first, and then use the merge function. The function should not assume that both its input parameter arrays are the same length but can assume that one array does not contain two copies of the same value. The result array should also contain no duplicate values. When one of the input arrays has been exhausted, do not forget to copy the remaining data in the other array into the result array. Test your function with three cases in below, The first array exhausted first Array -1.1, 2.5, 3.6, 5.5 Array2: -1 1, 2.6, 3.5, 3.5, 5.5, 6.3, 7.8. 9.9 The second array exhausted first: Arrayl: -10.5, -1.8, 3.5, 6.3, 7.2, 8.5 Array2: -1 S, 3.1. 6.3 The two arrays are exhausted at the same time Arrayl: -1.22.4, 3.3, 4.5, 5.5, 6.7, 8.8 Array2: 1, 2.3, 3.3, 4.6, 5.5, 5.9, S.S

Explanation / Answer

#include<stdio.h>

int printUnion(int arr1[], int arr2[], int m, int n)

{

int i = 0, j = 0;

while (i < m && j < n)

{

    if (arr1[i] < arr2[j])

      printf(" %d ", arr1[i++]);

    else if (arr2[j] < arr1[i])

      printf(" %d ", arr2[j++]);

    else

    {

      printf(" %d ", arr2[j++]);

      i++;

    }

}

/* Print remaining elements of the larger array */

while(i < m)

   printf(" %d ", arr1[i++]);

while(j < n)

   printf(" %d ", arr2[j++]);

}

/* Driver program to test above function */

int main()

{

int arr1[] = {1, 2, 4, 5, 6};

int arr2[] = {2, 3, 5, 7};

int m = sizeof(arr1)/sizeof(arr1[0]);

int n = sizeof(arr2)/sizeof(arr2[0]);

printUnion(arr1, arr2, m, n);

getchar();

return 0;

}

Algorithm Intersection(arr1[], arr2[]):

#include<stdio.h>

int printIntersection(int arr1[], int arr2[], int m, int n)

{

int i = 0, j = 0;

while (i < m && j < n)

{

    if (arr1[i] < arr2[j])

      i++;

    else if (arr2[j] < arr1[i])

      j++;

    else /* if arr1[i] == arr2[j] */

    {

      printf(" %d ", arr2[j++]);

      i++;

    }

}

}

/* Driver program to test above function */

int main()

{

int arr1[] = {1, 2, 4, 5, 6};

int arr2[] = {2, 3, 5, 7};

int m = sizeof(arr1)/sizeof(arr1[0]);

int n = sizeof(arr2)/sizeof(arr2[0]);

printIntersection(arr1, arr2, m, n);

getchar();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote