Write a function that will merge the contents of two sorted(ascending order) arr
ID: 3549502 • Letter: W
Question
Write a function that will merge the contents of two sorted(ascending order) arrays of type double values, storing the result in an array output parameter (still in ascending order). 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.
Hint. 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 cases in which (1) the first array is exhausted first, (2) the second array is exhausted first, and (3) the two arrays are exhausted at the same time (i.e, they end with the same value.) Rerember that the arrays input to this function must already be sorted.
Explanation / Answer
Replace the array a and b with your array. Change aLength and bLength according the length of arrays. You can also take it as input from user.
#include<stdio.h>
int main()
{
double a[50] = {1,2,5,10};
double b[50] = {3,4,6,12,15};
int c[50], cCounter = 0,i,aCounter,bCounter;
int aLength = 4, bLength = 5;
for( aCounter=0, bCounter=0;aCounter<aLength && bCounter<bLength; )
{
if(a[aCounter]<b[bCounter])
c[cCounter++] = a[aCounter++];
else
c[cCounter++] = b[bCounter++];
}
while(aCounter<aLength)
c[cCounter++] = a[aCounter++];
while(bCounter<bLength)
c[cCounter++] = b[bCounter++];
for( i=0;i<cCounter;i++)
printf("%d ",c[i]);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.