In C++ 1.Test your addNumbers by creating an empty vector in main( ) , call the
ID: 3680287 • Letter: I
Question
In C++1.Test your addNumbers by creating an empty vector in main(), call the addNumbers function, and use print_even to print your vector from main(). 2. Create a function computeSum(constvector<int>& data) function to add all numbers of your vector and returns the value. You must to use an iterator for this task. 3. Create a bool isPresent(constvector<int> &data, int value) function that returns true if the value is present in the datavector, and false otherwise. 4. Create a function void removerDuplicates(vector<int>& data) that removes all duplicate numbers (hint: use vector sort and unique). Search how they are implemented on vectors. Your new vector should include only the unique elements of data vector (hint: you may want to resize your vector!!) 5. Create a function void removeIf(vector<int>& data) to remove all numbers greater than 7 from your vector (use built-in function remove_if, see example ex2.cpp in your lectures). Note that remove_if for vectors is not the same as remove_if for lists. Click here to read about it. 6. Now in main, manually create a small vector of about 10numbers in random order and test all of your functions.
/Section: ee include cvector oid fillVector(vectorcint>&) oid addNumbers (vectorcint>&); oid printvector(vectorcint>&) rinting vector elemen cout cen Current Elemen cout
Explanation / Answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <stdio.h>
using namespace std;
void fillvector(vector<int>&);
void addNumbers(vector<int>&);
void printVector(vector<int>&);
void print_even(vector<int>&);
int computeSum(vector <int>&);
bool isPresent(vector<int>&, int);
void removerDuplicates(vector<int>&);
void removeIf(vector<int>&);
bool greater_than_7(int);
int main()
{
std::vector<int> data;
fillvector(data);
cout <<" Current Elements in the vector: ";
printVector(data);
cout<<" Adding new numbers to vector... ";
addNumbers(data);
cout<<" vector after adding numbers: ";
printVector(data);
cout<<" Printing vector Elements at even indxes: ";
print_even(data);
cout<<" Sum of all Elements in vector: " << computeSum(data) << endl;
cout<<" Enter value you want to find in vector: ";
int value;
cin >> value;
if(isPresent(data,value)) cout<<"Value is Present"<<endl;
else cout<<"Value id not Present"<<endl;
cout<<" Removing Duplicates.... ";
removerDuplicates(data);
cout<<" vector after removing Duplicates: ";
printVector(data);
cout<<" Removing all numbers greater than 7.... ";
removeIf(data);
cout<<" vector after removing all numbers greater than 7: ";
printVector(data);
cout<<" ";
return 0;
}
void fillvector(vector<int> &data)
{
int num,j;
srand(time(NULL));
for (j = 0; j < 10; ++j)
{
num = rand() %100 +1;
data.push_back(num);
}
}
void addNumbers(vector<int> &data)
{
int j, num;
for (j = 0; j < 10; ++j)
{
cout <<"Enter a number: ";
cin >> num;
data.push_back(num);
}
}
void printVector(vector<int> &data)
{
int j;
for (j = 0; j < data.size(); ++j)
{
cout <<" "<< data[j];
}
}
void print_even( vector<int> &data)
{
int j;
for (j = 0; j < data.size(); ++j)
{
if(j%2 == 0)
cout <<" "<< data[j];
}
cout<<" ";
}
int computeSum(vector <int> &data)
{
int sum = 0;
for(std::vector<int>::iterator it = data.begin(); it != data.end(); ++it)
sum += *it;
return sum;
}
bool isPresent( vector<int> &data, int value)
{
int j;
for (j = 0; j < data.size(); ++j)
{
if(data[j] == value) return true;
}
return false;
}
void removerDuplicates(vector<int> &data)
{
std::sort(data.begin(),data.end()); // suing sort
std::vector<int>::iterator it;
it = std::unique (data.begin(), data.end()); // using unique
data.resize( std::distance(data.begin(),it) ); // resize vector
std::unique (data.begin(), data.end());
}
bool greater_than_7(int value)
{
return (value>7);
}
void removeIf(vector<int>& data)
{
data.erase(std::remove_if(data.begin(), data.end(), greater_than_7),data.end());
std::vector<int>::iterator it;
it = std::unique (data.begin(), data.end());
data.resize( std::distance(data.begin(),it) );
}