write a program create a vector with random numbers. Use merge sortto reorder th
ID: 3671850 • Letter: W
Question
write a program create a vector with random numbers. Use merge sortto reorder the vector. Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the vector or replace a value in the vector.
Write your own functions for the program. I can only use the member function of the vector. Display the vector at every change. Do NOT use any global variable. Pass the vector and variables to the function appropriately.
Look in the following output as the guideline to write your program. We suggest you to write modular functions so they can be reused throughout the program and the future assignments.
Printing original vector: 3 4 7 6 4 7 6 7 4 7
Sorting using merge sort and print vector: 3 4 4 4 6 6 7 7 7 7
Enter a number to search in the vector (-1 to quit): 4
Number 4 found in the following indices: 1 2 3
Removing repeated elements of number 4 3 4 6 6 7 7 7 7
Enter a number to search in the vector (-1 to quit): 7
Number 7 found in the following indices: 4 5 6 7
Removing repeated elements of number 7 3 4 6 6 7
Enter a number to search in the vector (-1 to quit): 4
Number 4 found at index 1
Enter a number to search in the vector (-1 to quit): 5
Number 5 not found in the vector.
Select the following options:
1. Add number to vector.
2. Replace number in vector.
Input = 1
Vector after adding number 5
3 4 5 6 6 7
Enter a number to search in the vector (-1 to quit): 8
Number 8 not found in the vector.
Select the following options:
1. Add number to vector.
2. Replace number in vector.
2
Enter the number in the vector to be replaced: 7
Vector after replacing and sorting:
3 4 5 6 6 8
Enter a number to search in the vector (-1 to quit):
Explanation / Answer
#include #include #include #include using namespace std; class ReplaceFunc { int mNumComp; public: ReplaceFunc(int i):mNumComp(i){} bool operator()(int i) { if(i==mNumComp) return true; else return false; } }; int main() { int arr[]={3, 3, 6, 4, 9, 6, 1, 4, 6, 6, 7, 3}; std::vector vec(arr,arr + sizeof(arr)/sizeof(arr[0])); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.