Write function fillVector that takes as argument a vector. Clear all elements if
ID: 3706545 • Letter: W
Question
Write function fillVector that takes as argument a vector. Clear all elements if the vector size is not zero then ask the user to enter +ve int values and to enter a -ve value to stop. The function would add the entered int values to the vector.
Write function printVector that takes as argument a vector, and prints its elements. Always display the contents of the vector above the menu.
Write function reverseVector that takes as argument a vector and reverses the vector. You may not use the vector member function reverse().
Write function find that takes as argument a vector and an int value. It returns the index of the element or -1 if it is not found.
Write function removeIndex that takes as argument a vector and an index value. It removes the element at that index.
Write function removeValue that takes as argument a vector and an int value to remove. It removes that element if present. This function calls functions find and removeIndex.
Write function menu that prints the following menu: 1. fillVector
2. printVector
2. reverseVector 3. findValue
4. removeIndex 5. removeValue 6. quit
My program looks like this so far:
#include <iostream>
#include <vector>
using namespace std;
void menu();
void fillVector(vector<int> & numbers);
void showVector(vector<int> numbers);
int main() {
vector<int> numbers;
int choice = 0;
while(choice != 6){
showVector(numbers);
menu();
do{
cout<< "Select choice 1-6 : ";
cin>> choice;
}
while(choice < 0 || choice > 6);
if (choice == 1){
cout<< "Entere positive numbers into the list. Enter -1 to complete the list"<< endl;
fillVector(numbers);
}
}
}
void menu()
{
cout<< "1. fillVector ";
cout<<"2. reverseVector ";
cout<< "3. findValue ";
cout<< "4. removeIndex ";
cout<< "5. removeValue ";
cout<< "6. quit"<< endl;
}
void fillVector(vector<int> & numbers)
{
int nums;
int unum;
do{
cout<< "Enter number -1 to end program. ";
cin>> unum;
if( unum >= 0)
numbers.push_back(unum);
} while(unum >= 0);
}
void showVector(vector<int> numbers)
{
for( int i = 0; i < numbers.size(); i++){
if(numbers[i] < 1) {
cout<< numbers[i]<<" ";
}
else {
cout<< numbers[i]<< ", ";
}
}
cout<< endl;
}
void reverseVector(vector<int> & numbers){
vector<int>fillerVector;
for(int i = fillerVector.size() - 1; i >= 0; i--){
fillerVector.push_back(numbers[i]);
}
showVector(fillerVector);
}
Explanation / Answer
CODING:
#include<iostream>
using namespace std;
#include<vector>
void fillVector(vector<int> &v)
{
int x=3;
while(x>=0)
{
cout<<"Enter +ve values and a -ve value to indicate no more ";
cin>>x;
if(x>=0)
v.push_back(x);
}
}
void clearVector(vector<int> &v)
{
v.clear();
}
void printVector(vector<int> &v)
{
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
}
void reverseVector(vector<int> &v)
{
vector<int> temp;
for(int i=v.size()-1;i>=0;i--)
temp.push_back(v[i]);
v=temp;
}
void removeValue(vector<int> &v,int x)
{
int i;
for( i=0;i<v.size();i++)
{
if(v[i]==x)
{
break;
}
}
v.erase(v.begin()+i);
}
void removeIndex(vector<int> &v,int x)
{
v.erase(v.begin()+x-1);
}
void findValue(vector<int> &v,int x)
{
int z=0;
for(int i=0;i<v.size();i++)
{
if(v[i]==x)
{
z=1;
cout<<"Element found in vector ";
break;
}
}
if(z==0)
cout<<"Element not found in vector ";
}
main()
{
int choice=0;
vector<int> v;
while(choice!=8)
{
cout<<"Enter your Choice 1.fillVector 2.clearVector 3.printVector 4.reverseVector 5.removeValue 6.removeIndex 7.find 8.exit ";
cin>>choice;
switch(choice)
{
case 1:
{
fillVector(v);
break;
}
case 2:
{
clearVector(v);
break;
}
case 3:
{
printVector(v);
break;
}
case 4:
{
reverseVector(v);
break;
}
case 5:
{
int x;
cout<<"Enter value to be removed ";
cin>>x;
removeValue(v,x);
break;
}
case 6:
{
int x;
cout<<"Enter Index to be removed ";
cin>>x;
removeValue(v,x);
break;
}
case 7:
{
int x;
cout<<"Enter value to be find in vector ";
cin>>x;
findValue(v,x);
break;
}
case 8:
{
cout<<"Exiting ";
break;
}
default:
{
cout<<"Enter proper choice ";
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.