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...

  • 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...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to...

    In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to the beginning of an array of integers //Param 2: size of that array //Returns: true, is every element in the input array is a Fibonacci number //(the order of the numbers in the array need not be ordered //as per the Fibonacci sequence) //false, otherwise //A Fibonacci sequence is generated as follows. //The first two numbers in the sequence are 0 and 1. //The...

  • PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE...

    PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR IT. 5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above 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
ADVERTISEMENT