Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 17, Problem 3P
Program Plan Intro
Recursive Binary Search
Program Plan:
- Include the required header files.
- Declare the global variables.
- Declare the function template.
- Define main function.
- Declare the required variables.
- Get the array of elements.
- Display the given display.
- Sort the array in ascending order.
- Display the sorted array.
- Call the “search” function with the arguments.
- If the “found” value is true, display the search element location. Otherwise display, “the element is not found”.
- Define the “search” function
- Declare the variable
- If “first” is greater than “last”, set “found” value as “false”.
- Otherwise find the “mid” value.
- If the “key” value is equal to “a [mid]”, set “found” value as “true” and also set “location” as “mid”.
- If the “key” is less than “a [mid]”, call the “search” function with the arguments.
- Otherwise, call the “search” function with the arguments.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
(This question is for a linked list of the type described on the front page, Item 5). Write a recursive function CountGE60. that receives a linked list (may or may not be empty) and returns the number of nodes (each containing a data item value) greater than or equal to 60.
Write a recursive Java function displayOutIn that displays two items from an array list at one instance from out-to-in. until one or last two items displayed and the process stops. For example, given array A = {15, 74, 106, 10, 9, 86, 34} the displayOutIn function displays the following:
15, 34
74, 86
106, 9
10
Implements clone which duplicates a list. Pay attention, because if there are sublists, they must be duplicated as well. Understand the implementation of the following function, which recursively displays the ids of each node in a list
Develop your solution as follows:
First copy the nodes of the current list (self)
Create a new list with the copied nodes
Loop through the nodes of the new list checking the value field
If this field is also a list (use isinstance as in the show_ids function) then it calls clone on that list and substitutes the value.
Complete the code:
def L4(*args,**kwargs): class L4_class(L):
def clone(self):
def clone_node(node): return <... YOUR CODE HERE ...>
r = <... YOUR CODE HERE...> return r return L4_class(*args,**kwargs)
Chapter 17 Solutions
Problem Solving with C++ (9th Edition)
Ch. 17.1 - Write a function template named maximum. The...Ch. 17.1 - Prob. 2STECh. 17.1 - Define or characterize the template facility for...Ch. 17.1 - Prob. 4STECh. 17.1 - Display 7.10 shows a function called search, which...Ch. 17.1 - Prob. 6STECh. 17.2 - Give the definition for the member function...Ch. 17.2 - Give the definition for the constructor with zero...Ch. 17.2 - Give the definition of a template class called...Ch. 17.2 - Is the following true or false? Friends are used...
Ch. 17 - Write a function template for a function that has...Ch. 17 - Prob. 2PCh. 17 - Prob. 3PCh. 17 - Redo Programming Project 3 in Chapter 7, but this...Ch. 17 - Display 17.3 gives a template function for sorting...Ch. 17 - (This project requires that you know what a stack...Ch. 17 - Prob. 6PPCh. 17 - Prob. 7PPCh. 17 - This project requires that you complete...
Knowledge Booster
Similar questions
- By using JAVA, write a recursive display function, that print a linked list in reverse order.arrow_forwardWrite a template version of the iterative binary search algorithm from Display 13.8 which only searches an array of integers for an integer key. Specify requirements on the template parameter type. Discuss the requirements on the template parameter type.arrow_forward6.2 Create a Product class with Product Id & Product Name. Write a program to accept information of 10 products and store that in HashSet. Do following operations, a.Search a particular product in the HashSet.b.Remove a particular product from the HashSet by using product id.(Referbelow table for the product list) Product Id Product Name P001 Maruti 800 P002 Maruti Zen P003 Maruti Dezire P004 Maruti Altoarrow_forward
- Develop a class ResizingArrayQueueOfStrings that implements the queueabstraction with a fixed-size array, and then extend your implementation to use arrayresizing to remove the size restriction.Develop a class ResizingArrayQueueOfStrings that implements the queueabstraction with a fixed-size array, and then extend your implementation to use arrayresizing to remove the size restriction.arrow_forwardQ 4 Computer Science Write a Java generic Class with user-defined method(s) to exchange the positions of two different elements in an Array List.arrow_forwardImplements clone which duplicates a list. Pay attention, because if there are sublists, they must be duplicated as well. Understand the implementation of the following function, which recursively displays the ids of each node in a list def show_ids(M, level=0): k = M.first_node while k is not None: print(" "*2*level, id(k)) if (str(k.value.__class__.__name__) == str(M.__class__.__name__)): show_ids(k.value, level+1) k = k.next W=L2(Node(10, Node(L2(Node(14, Node(15, Node(L2(Node(16, Node(17))))))), Node(20, Node(30)))) )show_ids(W) Develop your solution as follows: - First copy the nodes of the current list (self) - Create a new list with the copied nodes - Loop through the nodes of the new list checking the value field - If this field is also a list (use isinstance as in the show_ids function) then it calls clone on that list and substitutes the value. Complete the code: def L4(*args,**kwargs): class L4_class(L):…arrow_forward
- In the Recursive Binary Search shown below, there are two base cases: Line 10: the search is exhausted without a finding a value that matches the key Line 16: the search finds a value that matches the key 1 public class RecursiveBinarySearch { 2 public static int binarySearch(int[] list, int key) { 3 int low = 0; 4 int high = list.length - 1; 5 return binarySearch(list, key, low, high); 6 } 7 8 private static int binarySearch(int[] list, int key, 9 int low, int high) { 10 if (low > high) // The list has been exhausted without a match 11 return -low - 1; 12 13 int mid = (low + high) / 2; 14 if (key < list[mid]) 15 return binarySearch(list, key, low, mid - 1); 16 else if (key == list[mid]) 17 return mid; 18 else 19 return binarySearch(list, key, mid + 1, high); 20 } 21 22 public static void main(String[] args) { 23 int[] list = {3, 5, 7, 8, 12, 17,…arrow_forwardImplement InsertionSort function either iteratively or recursively: For iterative implementation: // You are required to use BinarySearch in your implementation of InsertionSort void InsertionSort (vector<int> & list) For recursive implementation // You are required to use BinarySearch in your implementation of InsertionSort void InsertionSort (vector<int> & list, int first, int last)arrow_forwardRewrite the Binary search function in the textbook to use a generic type for the array elements. Test the function with arrays of int, double, and string values. *When using test arrays make sure elements are stored in ascending sorted order*arrow_forward
- Given the following non-recursive implementation of depth-first search: B. What is the purpose of stack<string> q? C. What is the purpose of set<string> v?arrow_forwardImplement the function below void swap(int pos1, int pos2) {} Inital code to be completed: class LinkedList : public List { node* head; node* tail; int index; node* create_node(int num) { node* n = (node*) malloc(sizeof(node)); n->element = num; n->next = NULL; return n; } public: LinkedList() { index = 0; head = NULL; tail = NULL; } int add(int num) { addTail(num); return index; } int get(int pos) { node* currnode = head; int count =0; while (currnode != NULL){ count++; if (count == pos){ return currnode -> element; }else{ currnode = currnode -> next; } } return -1; } int size() { return index; } void swap(int pos1, int pos2) { } // WARNING!…arrow_forwardImplement the Plates class buildMap function so that it populates the HashMap with the state abbreviations as keys and the counts of how many each appear in the file as values. Sometimes, the parking attendant will add special notation to help her remember something about a specific entry. There are just non alphabetic characters that she adds to the state - your program should ignore these characters so that an entry like NY* still counts toward the NY plate count. She is also very inconsistent with how she enters the plates. Sometimes she uses upper case, sometimes lowercase, and sometimes she even uses a mix. Be sure to account for this in your program. Only add information for plates in New England (Maine, New Hampshire, Vermont, Massachusetts, Rhode Island, and Connecticut). Plates.java import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Plates { private Map<String, Integer> plateMap;…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning