I wrote 2 functions:
PrintIdentical -receives 2 arrays and their sizes – sorts them and than call to function
bin_search_print that needs to search each element from the small array inside the big array and print it if found.
i have a bug – the bin_search_print function found the first element and print the first element in a loop and not reaching the other elements.
void bin_search_print(int key,int *a,int n)
{
int low,high,mid;
low=0;
high=n-1;
while(low <= high)
{
mid=(low+high)/2;
if(key==a[mid])
printf ("%d", mid);
return mid;
else if(key<a[mid])
high=mid-1;
else /* key >a[mid] */
low=mid+1;
}
return -1;
}
void PrintIdentical(int arrA[], int arrA_size, int arrB[], int arrB_size)
{
int i;
int smaller;
int *newarr;
merge_sort(arrA, 0, arrA_size);
merge_sort(arrB, 0, arrB_size);
if(arrA_size>arrB_size)
{
smaller=arrB_size;
for(i = 0; i < smaller; i++) {
bin_search_print(arrB[i], arrA, arrA_size+1);
}
}
else
{
smaller=arrA_size;
for(i = 0; i < smaller; i++) {
bin_search_print(arrA[i], arrB, arrB_size+1);
}
}
}
A[1,2,3,4,5]
B[1,4,3,9]
I need to print 1 4 3
bin_search_print is missing a return after the printf.
Additional notes:
bin_search_print is declared as void and should not return a value.
Please use curly braces with your
ifstatements!Please turn on compiler warnings.