Due tonight at midnight, please help!! -Write a program create a vector with ran
ID: 3671770 • Letter: D
Question
Due tonight at midnight, please help!!
-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 <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
// compare only integral part:
bool mycomparison (double first, double second)
{ return ( int(first)<int(second) ); }
void random_fill(vector<int> &data)
{
int i,initial = 10; //initial size of vector is 10
srand(time(NULL));
for(i=0;i<initial;i++)
{
data.push_back(rand()%10+2);
}
}
void print(vector<int> data)
{
int i;
for(i=0;i<data.size();i++)
cout<<data[i]<<" ";
cout<<endl;
}
bool search(vector<int> &data, int n)
{
vector<int>::iterator it;
vector<int> found;
int index = 0;
for(it=data.begin() ; it < data.end(); it++,index++)
if(*it == n)
found.push_back(index); //storing all the indices of n
if(found.size()!=0) //if n is found
{
cout<<"Number "<<n<<" found in the following indices: ";
print(found);
if(found.size()!=1) //if there is only one occurence of element then no need to print below
{
cout<<"Removing repeated elements of number "<<n<<" Modified vector: ";
index = 0;
for(it=found.begin() + 1 ; it < found.end(); it++, index++) //found.begin() + 1 to Leave first occurence of n, I am not erasing the first occurence of n
data.erase(data.begin() + *it - index); //erasing all duplicate occurence of n
print(data);
}
return true;
}else
{
cout<<"Number "<<n<<" not found in the vector ";
return false;
}
}
void add(vector<int> &data, int n)
{
data.push_back(n);
sort(data.begin(), data.begin()+data.size());
cout<<"Vector after adding number "<<n<<" Modified array: ";
print(data);
}
void replace(vector<int> &data, int replace)
{
int n,index;
while(true)
{
cout<<"Enter the number in the vector to be replaced: ";
cin>>n;
//This function replaces all occurenece of "n" with replace"
if(find(data.begin(), data.end(), n)!=data.end())
break;
else
{
cout<<n<<" not found in vector. Please enter valid number ";
continue;
}
}
vector<int>::iterator it;
for(it=data.begin(), index = 0 ; it < data.end(); it++, index++) {
if(*it==n)
data[index] = replace;
}
sort(data.begin(), data.begin()+data.size());
cout<<"Vector after replacing and sorting: ";
print(data);
}
int main()
{
vector<int> data;
int choice,n;
random_fill(data);
cout<<"Printing original vector: ";
print(data);
cout<<"Sorting using merge sort and print vector: ";
sort(data.begin(), data.begin()+data.size()); //sort() function in algorithm is by default merge sort
print(data);
while(true)
{
cout<<"Enter a number to search in the vector ( 1 to quit): ";
cin>>n;
if(n==1)
break;
if(!search(data,n))
{
cout<<" Select the following options: 1. Add number to vector. 2. Replace number in vector. Enter your choice: ";
cin>>choice;
if(choice==1)
{
add(data,n);
}else if(choice==2)
{
replace(data,n);
}else
{
cout<<"Wrong choice entered. Please try again!! ";
continue;
}
}
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.