Please use this template.
In this exercise you are to use a vector to store integers entered by the user. Once you have filled the vector, ask the user for a value to search for. If found, remove the value from the vector, keeping all other values in the same relative order, and then display the remaining values.
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
//Fills vector with user input until user enters 0 (does not
include 0 within vector).
void fillVector(vector<int> &v);
//Searches for val within vector.
//If val found, returns index of first instance of val.
//If val not found, returns UINT_MAX (constant provided by the
climits library).
unsigned search(const vector<int> &v, int val);
//Removes from the vector the value at index, keeping all other
values in the same relative order.
void remove(vector<int> &v, unsigned index);
//Displays all values within vector, each value separated by a
space.
void display(const vector<int> &v);
int main()
{
vector<int> v;
int value;
fillVector(v);
cout << "Enter value to search for: ";
cin >> value;
//search for value
unsigned pos = search(v, value);
cout << "Found: ";
//if val found, output position it was found and then remove it
from vector
//otherwise output "NOT FOUND"
//UINT_MAX is a constant provided by the climits library (see
http://www.cplusplus.com/reference/climits/ for more info)
if (pos != UINT_MAX)
{
cout << pos << endl;
remove(v, pos);
}
else
{
cout << "NOT FOUND" << endl;
}
cout << "Result: ";
//output the vector's values.
display(v);
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
//Fills vector with user input until user enters 0 (does not
include 0 within vector).
void fillVector(vector<int> &v)
{
int input;
cout << "Enter a value." << endl;
while((cin >> input) && input !=
0)
{
v.push_back(input);
}
}
//Searches for val within vector.
//If val found, returns index of first instance of val.
//If val not found, returns UINT_MAX (constant provided by the
climits library).
unsigned search(const vector<int> &v, int val)
{
for(unsigned i = 0; i < v.size(); i++)
{
if(v.at(i) == val)
{
return i;
}
}
return UINT_MAX;
}
//Removes from the vector the value at index, keeping all other
values in the same relative order.
void remove(vector<int> &v, unsigned index) //index is
the value of the returned i from search
{
while(index + 1 < v.size()) //while the index is in
the size of the vector
{
v.at(index) = v.at(index+1);
index++;
} //moves value of found int to the end of the vector;
the index++ makes sure that the index keeps moving up until it hits
the end
if(index < v.size() && index !=
UINT_MAX)
{
v.pop_back();
} //since the index's value is at the end, we
pop_back()
}
//Displays all values within vector, each value separated by a
space.
void display(const vector<int> &v)
{
for(unsigned i = 0; i < v.size(); i++)
{
cout << v.at(i) << '
';
}
}
int main()
{
vector<int> v;
int value;
fillVector(v);
cout << "Enter value to search for: ";
cin >> value;
//search for value
unsigned pos = search(v, value);
cout << "Found: ";
//if val found, output position it was found and then
remove it from vector
//otherwise output "NOT FOUND"
//UINT_MAX is a constant provided by the climits
library (see http://www.cplusplus.com/reference/climits/ for more
info)
if (pos != UINT_MAX) //if the search() function
doesn't return UINT_MAX, we can use remove()
{
cout << pos <<
endl;
remove(v, pos);
}
else
{
cout << "NOT FOUND" <<
endl; //if search() returns UINT_MAX, we cout not found
}
cout << "Result: ";
//output the vector's values.
display(v);
cout << endl;
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.