Question

Question 10 15 pts In this question, you are asked to implement a recursive function that checks whether two increasingly sor
Increasingly sorted array of integers and checks whether the input integer key exists in the given array *** A correct non-re
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN C++ :

#include<iostream>
using namespace std;
int binarysearch(int key, int* array, int size) // given in the question
{
int low = 0, high = size-1;
while(low < high)
{
if(array[(low+high)/2] == key)
return 1;
else if(array[(low+high)/2] > key)
high = (low+high)/2;
else
low = (low+high)/2 + 1;
}
return 0;
}
int isDisjoint(int* sorted1, int size1, int* sorted2, int size2)
{
for(int i = 0; i < size1; i++)
{
if(binarysearch(sorted1[i], sorted2, size2) == 1) // searching for every element of sorted1 in sorted2 until we get any common
{
return 1;
}
}
return 0;
}

OUTPUT SNIPPET

Case 1 : Disjoint

int main()
{
int array1[5] = {2,4,6,8,10};
int array2[5] = {1,3,5,7,9};
if(isDisjoint(array1,5,array2,5) == 0)
{
cout << "The arrays are disjoint" << endl;
}
else
{
cout << "The arrays are not disjoint" << endl;
}
}

The arrays are disjoint Process returned 0 (0x0) Press any key to continue. execution time : 9.085 s

Case 2 : Not disjoint

int main()
{
int array1[5] = {2,4,6,8,10};
int array2[5] = {1,3,6,7,9};
if(isDisjoint(array1,5,array2,5) == 0)
{
cout << "The arrays are disjoint" << endl;
}
else
{
cout << "The arrays are not disjoint" << endl;
}
}

The arrays are not disjoint Process returned (ØxO) execution time : 0.078 s Press any key to continue.

Add a comment
Know the answer?
Add Answer to:
Question 10 15 pts In this question, you are asked to implement a recursive function that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

  • Write a recursive function that computes the smallest integer in a given array of integers. Use...

    Write a recursive function that computes the smallest integer in a given array of integers. Use the following algorithm: int min(int[] A, int low, int high) { if (low == high) return A[low]; int mid = (low + high) / 2; int min1 = min(A, low, mid); int min2 = min(A, mid + 1, high); if (min1 > min2) return min2; return min1; }

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

  • In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...

    In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • Implementing a recursive function that calculates the remainder of dividing a very large integer by three...

    Implementing a recursive function that calculates the remainder of dividing a very large integer by three Implementing a recursive function that calculates the remainder of dividing a very large integer by three 1 .) Remainder of Dividing by Three The easiest way to find the remainder of a given integer like 235 when dividing by three is to calculate the remainder of 2 + 3 + 5 instead. Use this idea to implement a recursive function that calculates the remainder...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT