CAN SOMEONE FILL OUT THE PARTS OF THE CODE THAT SAY TO DO: THANK YOU!! #include
ID: 3590201 • Letter: C
Question
CAN SOMEONE FILL OUT THE PARTS OF THE CODE THAT SAY TO DO: THANK YOU!! #include <iostream> #include <assert.h> #include <vector> #include <string> using namespace std; void DisplayVector (vector vstrings); bool BinarySearch (vector vstrings, string value, int & index); void Delete (vector& vstrings, int index); void Delete (vector& vstrings, string value); int InsertByValue (vector& vstrings, string value); int main() { vector vstrings; // a string vector string temp_str; do { cout << "Enter another name or -1 to end input: "; cin >> temp_str; if (temp_str != "-1") { InsertByValue(vstrings, temp_str); } } while (temp_str != "-1"); DisplayVector(vstrings); //2. TODO: Prompt the user to enter a value to search for, search for the value, and // report the result // 3. ToDo: Call your function to insert a value (entered by the user) into the vector // // Display the content of the vector afterwards // 4. ToDo: Prompt the user to enter an index (for the value to be deleted), and then delete the elements // stored there. // Display the content of the vector after the deletion. // 5. ToDo: Prompt the user to enter an value in the vector (to be deleted), and then delete the element // Display the contents of the vector after the deletion. // BONUS: implement sorting vector. vector another_vector={"becky","ann","karen","julie", "frances"}; //6. ToDo: call you sorting function to sort another_vector into ascending order } //TODO: Implement all functions, i.e., write the function definitions here ... void DisplayVector (vector vstrings) { // uses for-loop to iterate and display. BONUS: Use iterator vstrings.begin and vstrings.end } bool BinarySearch (vector vstrings, string value, int & index) { } void Delete (vector& vstrings, int index) { // uses vector.erase(index) } void Delete (vector& vstrings, string value) { // BinarySearch to get index and vector.erase(index) to // delete. } int InsertByValue (vector& vstrings, string value) { // Uses for-loop to find the vector enter that is greater than value // if we get to the end, do vector.push_back(value) otherwise use // vector.insert(vector.begin()+index, value); }
Explanation / Answer
#include <iostream>
#include <assert.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool BinarySearch (vector <string> vstrings, string value, int & index);
void Delete (vector <string>& vstrings, int index);
void Delete (vector <string>& vstrings, string value);
void DisplayVector (vector <string> vstrings);
int InsertByValue (vector <string>& vstrings, string value);
int main()
{
vector <string> vstrings; // a string vector
string temp_str;
do {
cout << "Enter another name or -1 to end input: ";
cin >> temp_str;
if (temp_str != "-1") {
InsertByValue(vstrings, temp_str);
}
} while (temp_str != "-1");
DisplayVector(vstrings);
//2. TODO: Prompt the user to enter a value to search for, search for the value, and
// report the result
cin >> temp_str;
int ind;
if(BinarySearch(vstrings, temp_str, ind))
{
cout << "found at index: " << ind;
}
// 3. ToDo: Call your function to insert a value (entered by the user) into the vector
//
// Display the content of the vector afterwards
cin >> temp_str;
InsertByValue(vstrings, temp_str);
DisplayVector(vstrings);
// 4. ToDo: Prompt the user to enter an index (for the value to be deleted), and then delete the elements
// stored there.
// Display the content of the vector after the deletion.
int index;
cin >> index;
Delete(vstrings, index);
DisplayVector(vstrings);
// 5. ToDo: Prompt the user to enter an value in the vector (to be deleted), and then delete the element
// Display the contents of the vector after the deletion.
cin >> temp_str;
InsertByValue(vstrings, temp_str);
Delete(vstrings, temp_str);
// BONUS: implement sorting vector.
vector <string> another_vector={"becky","ann","karen","julie", "frances"};
//6. ToDo: call you sorting function to sort another_vector into ascending order
std::sort(another_vector.begin(), another_vector.end());
DisplayVector(another_vector);
}
//TODO: Implement all functions, i.e., write the function definitions here ...
void DisplayVector (vector <string> vstrings)
{
// uses for-loop to iterate and display. BONUS: Use iterator vstrings.begin and vstrings.end
vector <string> :: iterator i;
for (i = vstrings.begin(); i != vstrings.end(); ++i)
cout << *i << ' ';
}
bool BinarySearch (vector <string> vstrings, string value, int & index)
{
std::sort(vstrings.begin(), vstrings.end());
vector <string> :: iterator first = vstrings.begin();
vector <string> :: iterator begin = vstrings.begin();
vector <string> :: iterator element = vstrings.begin();
vector <string> :: iterator last = vstrings.end();
bool ret = false;
size_t p = 0;
size_t shift = 0;
while((first <= last))
{
p = std::distance(begin, first);
size_t u = std::distance(begin, last);
size_t m = (p+u)/2;
std::advance(element, m - shift);
shift = m;
if(*element == value)
{
index = m; // value found at position m
ret = true;
break;
}
if(value > *element)
first = element++;
else
last = element--;
}
// if you are here the value is not present in the list,
// however if there are the value should be at position u
// (here p==u)
return ret;
}
void Delete (vector <string>& vstrings, int index)
{
// uses vector.erase(index)
vstrings.erase (vstrings.begin()+index);
}
void Delete (vector <string>& vstrings, string value)
{
// BinarySearch to get index and vector.erase(index) to
// delete.
int index;
if(BinarySearch(vstrings, value, index))
{
vstrings.erase(vstrings.begin()+index);
}
}
int InsertByValue (vector <string>& vstrings, string value)
{
// Uses for-loop to find the vector enter that is greater than value
// if we get to the end, do vector.push_back(value) otherwise use
// vector.insert(vector.begin()+index, value);
vstrings.push_back(value);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.