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

Part #2 (Array manipulation): Write a function that for given two arrays (input

ID: 3829158 • Letter: P

Question

Part #2 (Array manipulation): Write a function that for given two arrays (input arrays) returns a new array (output array) that contains the unique elements of the input arrays, in sorted order. The result array may not contain any duplicates (though each array may have some of the same values).

Hints: Remember to pass sizes of input arrays into this function. Also, recall how arrays are returned from the function. More specifically, what is default mechanism for passing arrays into a funtion. You may want to combine the arrays before sorting the final result.

Example:

Input Array #1:

intput Array #2:

Output Array:

Note: You do not need to return the size of the output array. You may assume that the size of the output array is always valid.

1 15 38 83 22 23 10 20 79 80 55

Explanation / Answer

#include<stdio.h>
main()
{
int array1[30],array2[30],result_array[60];
int i,j,k,m,n;
printf("enter no.of elements in array1:"); //reads no. of elements in array1
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&array1[i]); //reads the elements in array1
}
printf("enter no.of elements in array2:"); //reads no. of elements in array2
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&array2[i]); //reads the elements in array2
}
i=0;
j=0;
k=0; // initailize the variables to 0 to avoid garbage values
while (i<m && j<n)
{
if(array1[i]<=array2[j]) //checks the value of array1's elements with array2's elements
{
result_array[k]=array1[i]; //if array1's element is < then it is sent to result_array
i++; //increments array1
k++; //increments result_array
}
else
{
result_array[k]=array2[j]; //if array2's element is < then it is sent to result_array
j++; /increments array2
k++; //increments result_array
}
}
while(i<m) //if there are any elements left in array1 after sorting sent to result_array
{
result_array[k]=array1[i];
i++;
k++;
}
while(j<n) //if there are any element in array2 left after completion of sorting it is sent to result_array
{
result_array[k]=array2[j];
k++;
j++;
}
printf("resultant array is"); //prints result
for(i=0;i<m+n;i++); //m+n elements
printf("%d",result_array[i]);
return(0);
}