Concept explainers
Can you help me with my code? It is only giving me one error saying my "identifier: binary_search is unidentified", and I can't figure out how to fix it.
#include <iostream>
#include <string>
#include <
using namespace std;
template <class T>
class myVector : public vector<T> {
bool sorted = false; // static member to remember if the list is sorted
public:
int seqSearch(T searchItem);
int binarySearch(T searchItem);
void bubbleSort();
void insertionSort();
};
int main() {
// Define test vector
myVector<string> nameList;
// Add values to the vector
nameList.push_back("Alice");
nameList.push_back("Bob");
nameList.push_back("Eve");
nameList.push_back("Charlie");
nameList.push_back("Dave");
// Test sort methods
nameList.bubbleSort();
nameList.insertionSort();
// Test search methods
int index = nameList.seqSearch("Charlie");
cout << "Charlie was found at index: " << index << endl;
index = nameList.binarySearch("Charlie");
cout << "Charlie was found at index: " << index << endl;
// Print sorted vector using range based for loop
cout << "Sorted vector: ";
for (const auto& name : nameList) {
cout << name << " ";
}
cout << endl;
// Define new test vector
myVector<int> intList;
// Define an iterator to the vector
auto it = intList.begin();
// Add values to the vector
intList.insert(it, 1);
intList.insert(it, 3);
intList.insert(it, 2);
intList.insert(it, 5);
intList.insert(it, 4);
// Test the STL binary_search algorithm
bool found = binary_search(intList.begin(), intList.end(), 3);
if (found) {
cout << "3 was found in the vector." << endl;
}
else {
cout << "3 was not found in the vector." << endl;
}
// Print the resulting vector using an iterator
cout << "Sorted vector: ";
for (it = intList.begin(); it != intList.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
template <class T>
int myVector<T>::seqSearch(T searchItem) {
// Sequential search implementation
for (int i = 0; i < this->size(); i++) {
if (this->at(i) == searchItem) {
return i; // return index of found item
}
}
return -1; // return -1 if item not found
}
template <class T>
void myVector<T>::bubbleSort() {
// Bubble sort implementation
bool swapped = true;
while (swapped) {
swapped = false;
for (int i = 0; i < this->size() - 1; i++) {
if (this->at(i) > this->at(i + 1)) {
// Swap values
T temp = this->at(i);
this->at(i) = this->at(i + 1);
this->at(i + 1) = temp;
swapped = true;
}
}
}
sorted = true; // set sorted to true after sorting
}
template <class T>
void myVector<T>::insertionSort() {
// Insertion sort implementation
for (int i = 1; i < this->size(); i++) {
T value = this->at(i);
int j = i - 1;
while (j >= 0 && this->at(j) > value) {
this->at(j + 1) = this->at(j);
j--;
}
this->at(j + 1) = value;
}
sorted = true; // set sorted to true after sorting
}
template <class T>
int myVector<T>::binarySearch(T searchItem) {
// Binary search implementation
if (!sorted) { // check if list is sorted
bubbleSort(); // sort list if not sorted
}
int low = 0;
int high = this->size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (this->at(mid) == searchItem) {
return mid; // return index of found item
}
else if (this->at(mid) > searchItem) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return -1; // return -1 if item not found
}
int main() {
// Define test vector
myVector<string> nameList;
// Add values to the vector
nameList.push_back("Alice");
nameList.push_back("Bob");
nameList.push_back("Eve");
nameList.push_back("Charlie");
nameList.push_back("Dave");
}
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 5 images
- Use stack concepts Q #2 Ô https://www.loc-cs.org/~chu/DataStructu Essay Questions (20% each)- continue [0] [1] [2] [3] myList "Bashful" "Awful" Jumpy "Happy The above "myList" is an ArrayList in Java program. 2. If I want to remove "Awful" from myList, which of the following actions should I take first? a. Move "Happy" to the previous element first. b. Move "Jumpy" to the previous element first. Describe the reason of your choice. Next Pagearrow_forwardImplement the following: Given: person = {'name': 'bob'}1) Add the key/value pair 'emplStatus': 'Retired' to the dictionary person.2) Access the values in the dictionary person to print the following. Example OutputBob's employment status is Retired. NEED HELP WITH PYTHONarrow_forward#include #include using std::cout; using std::vector; class StatSet { private: HCreate a vector of doubles that is called "numbers." public: //Nothing to do in the constructor. StatSet() } // Add a new number to the set. vòid add_num(double num) { } double mean() { //Sum up the values and return the average. double sum = 0; //Write a loop that would iterate through the vector. sum += numbers[i]; return sum / numbers.size(); } double median() //Before calculating the median, we must sort the //array.arrow_forward
- #include <iostream>#include <vector>using namespace std; void PrintVectors(vector<int> numsList) { unsigned int i; for (i = 0; i < numsList.size(); ++i) { cout << numsList.at(i) << " "; } cout << endl;} int main() { vector<int> numsList; int userInput; int i; for (i = 0; i < 3; ++i) { cin >> userInput; numsList.push_back(userInput); } numsList.erase(numsList.begin()+1); numsList.insert(numsList.begin()+1, 102); numsList.insert(numsList.begin()+1, 100); PrintVectors(numsList); return 0;} Not all tests passed clearTesting with inputs: 33 200 10 Output differs. See highlights below. Special character legend Your output 33 100 102 10 Expected output 100 33 102 10 clearTesting with inputs: 6 7 8 Output differs. See highlights below. Special character legend Your output 6 100 102 8 Expected output 100 6 102 8 Not sure what I did wrong but the the 33…arrow_forward] ] get_nhbr In the cell below, you are to write a function called "get_nhbr(Ist, graph)" that takes in two inputs: a list of vertices and a graph. The function is to return a list that contains the neighborhood of the vertices in 'Ist' (remember that this means you are finding the union of the individual vertices' neighborhoods). + Code + Markdown After compiling the above cell, you should be able to compile the following cell and obtain the desired outputs. print (get_nhbr(["A", "D"], {"A" : ["B"], "B" : ["A", "D", "E"], "C" : ["E"], "D":["B"], "E":["B","C","F"], "F":["E"]}), get_nhbr(["B", "C", "F"], {"A" : ["B"], "B" : ["A", "D", "E"], "C" : ["E"], "D":["B"], "E":["B","C","F"], "F":["E"]})) This should return ["B"] ["A", "D", "E"] Python Pythonarrow_forward#include using namespace std; struct ListNode { string data; ListNode *next; }; int main() { ListNode *p, *list; list = new ListNode; list->data = "New York"; p new ListNode; p->data = "Boston"; list->next = p; p->next = new ListNode; p->next->data = "Houston"; p->next->next = nullptr; // new code goes here Which of the following code correctly deletes the node with value "Boston" from the list when added at point of insertion indicated above? O list->next = p; delete p; O p = list->next; %3D list->next = p->next; delete p; p = list->next; list = p->next; delete p; O None of these O p = list->next; %3D list->next = p; %3D delete p;arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education