This program will use 2 different data structures to perform the same tasks; a v
ID: 3681013 • Letter: T
Question
This program will use 2 different data structures to perform the same tasks; a vector and a dynamically sized array
Write a complete C++ program that will:
Declare a vector of integers
Use a pointer to dynamically allocate an array of 10 elements
Ask the user how many values they would like to have in the data structures
Read in the user’s response and make sure that it is greater than 20 (error loop) Generate the number of integers that the user asked for and store them into both data
structures. The integer values should be unique unordered values (no duplicate
values). Inside a loop:
Present the user with a menu with the following choices: Output all values to the screen
Add a new data value
Remove a data value
Sort the data Quit
Have the program perform the chosen menu option and then repeat the menu until the user chooses to quit.
Perform all operations (input, output, sort, add, remove) on both data structures.
Output: show 10 values per line with regular spacing
Add: ask the user for an integer value.
search the data structure (either one) for that value
if the value is NOT already in the data structures, then add it to both if the value is already in the data structures, do nothing
Remove:
Ask the user for an integer value
If the value is found in the data structures, remove it from both If the value is not currently in the data structures, do nothing
Sort: sort the data in both data structures from small to large
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main(){
vector<int> vec;
int *array=new int;
int num;
map<int,int> m;
cout<<"Enter the number of input values"<<endl;
cin>>num;
while(num<=20){
cout<<"Enter a value greater than 20"<<endl;
cin>>num;
}
cout<<"Enter "<<num<< " value :"<<endl;
int i=0;
for(i=0;i<num;++i){
int temp;
cin>>temp;
if(m[temp]==0){
vec.push_back(temp);
array[i]=temp;
m[temp]=1;
}
}
int input;
while(1){
cout<<"Press 1 : Output all values to the screen"<<endl;
cout<<"Press 2 : Add a new data value"<<endl;
cout<<"Press 3 : Remove a data value"<<endl;
cout<<"Press 4 : Sort the data"<<endl;
cout<<"Press 5 : Quit"<<endl;
cin>>input;
if(input==1){
int j=0;
for(j=0;j<vec.size();++j){
if(j>0 && j%10==0){
cout<<endl;
cout<<vec[j]<<" ";
}
else
cout<<vec[j]<<" ";
}
cout<<endl;
}
else if(input==2){
int temp;
cin>>temp;
if(m[temp]==0){
vec.push_back(temp);
array[i++]=temp;
m[temp]=1;
cout<<"New data added"<<endl;
}
}
else if(input==3){
int temp;
cin>>temp;
if(m[temp]==1){
vec.erase( remove(vec.begin(), vec.end(), temp), vec.end() );
m[temp]=0;
cout<<"Input data removed"<<endl;
}
}
else if(input==4){
sort(vec.begin(),vec.end());
cout<<"Sorted!!!"<<endl;
}
else if(input==5){
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.