Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The below given C++ code implements the algorithm to find the immediate successor of the entered target in the given array anArray[]. The code has been properly commented to enable the reader to understand the code better.

Explanation:

The binarySuccessor(const int anArray[], const int first, const int last, int target) takes in an array with its first and the last element's indices along with the target element that needs to be checked against the provided array to find its immediate successor.

The function first checks if the firstindez > lastinden OR the element at the array's last index is smaller than the target. If either of the conditions is true then the function returns -1 because if the first element's index cannot exceed the last element's index and as the array is sorted in ascending order, the last element cannot be smaller than the target for the immediate successor for the target to exist.

Next, the function checks if thefirstindez-lastindenAND the element at this index is \geqtarget. If this case is satisfied then the function returns this index as it is this element that the search has narrowed down to and this element is also \geqtarget.

If these above conditions are not true then the function follows the binary search algorithm to find the immediate successor.

First, the mid of the array is found and the element at mid is equated with the target element. If the element at mid < target then the search space in the array shrinks to (mid + 1), last] as the array is in ascending order and no other element smaller than the element at mid can be greater than or equal to the target.

If the element at mid > target then the search space in the array shrinks to first, mid, as we need to find the smallest number in the array which is greater than or equal to the target element. We also include the mid element in our search space here as the mid element is also greater than the target element and can be a part of the answer.

The function by default returns -1 when no condition is satisfied.

C++ Code:

1 2 #include <bits/stdc++.h> using namespace std; 4 5 /* @param: const int anArray[]:the array in ascending order containing

24 25 26 27 28 29 30 31 32 // Else the immediate successor is located according to the binary search algorithm else f int mid48 49 50 51 52 53 54 for(int i = i printf(%d , < last ; i++) anArray[1]); ; printf(In) 56 main) is the driver function th73 74 75 return 0

Sample output:

1.

he array: 12 3 56711 arget: 10 eturned index: 6

2.

3.

The array: -10 2 3 5 arget: 4 eturned index: 3

4.

he array: -10 2 3 5 arget: 100 eturned index: -1

Add a comment
Know the answer?
Add Answer to:
PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE...
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
  • Please help with this coding 1.You need to dynamically allocate memory to read into a number...

    Please help with this coding 1.You need to dynamically allocate memory to read into a number of elements. At first, you need to determine how many numbers (here, ‘n’) to be read. Next, you need to dynamically allocate memory for ‘n’ integers. As you see,‘x’ is an integer pointer which needs to dynamically allocate memory to read ‘n’integers into it fromthekeyboard. 2 Read nintegers into the allocated block using scanf. 3. Complete the printArrayfunction to display ‘n’ integers. void printArray(int...

  • Please implement it using recursion and it would be great if you could provide test code...

    Please implement it using recursion and it would be great if you could provide test code as well. Thanks. 1) Implement exponentiation recursively based on the following mathematical facts , if n = even number m" = 2 1-1 1-1 n-1 2 mxm т × m T = m × (mT) If n = o a a number , You can assume n is a non-negative integer. int expo(const int m, const unsigned int n)

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Please answer in C++, and Please consider ALL parts of the question, especially where it asks the...

    Please answer in C++, and Please consider ALL parts of the question, especially where it asks the user for V. So there should be a "cin >> V" somewhere. The last guy did not answer it correctly so please make sure you do! Thank You!! Question 1 Write the following two functions. The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second...

  • STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...

    STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...

  • STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...

    STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

  • Need Part 1 and Part 2 please! also has to build and run, thank you! CSCI...

    Need Part 1 and Part 2 please! also has to build and run, thank you! CSCI 40 Computer Programming Methodology I Assignment 16 Part l Write a program that declares a two dimensional array and initialize it with following integers 2 4 3 81 45 2 10 3 6 20 21 12 28-5 7 11 2 4 75041 8 5 -10 3 2 0 Then 1. Write statements that display all the numbers in the array 2. Write statements that...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

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