C++ Programming In this exercise you are to use a vector to store integers enter
ID: 3661588 • Letter: C
Question
C++ Programming
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.
Please Use This Code Template! Your help is greatly appreciated, thank you.
#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;
}
Explanation / Answer
#include<iostream>
#include<conio.h>
using namespace std;
template <class T>
void s_sort(T a[],int n)
{
int i,j,t,t1;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"Enter The number of Element: ";
cin>>n;
cout<<" Enter Elements: ";
for(i=0;i<n;i++)
{
cout<<" Enter:";
cin>>a[i];
}
s_sort(a,n);
cout<<" After Sorting ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<" Enter the location where you wish to delete element ";
cin>>t1;
if(t1>=n+1)
cout<<"deletion not possible ";
else
{
for(i=t1-1;i<n-1;i++)
array[i]=array[c+1];
cout<<"resultant array is ";
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<array[i];
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.