Using Visual studio 2010(C++) This program will use 2 different data structures
ID: 3681132 • Letter: U
Question
Using Visual studio 2010(C++)
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 All of the major array operations must be done in functions. If vector operations take more than one or two lines, they should also be done in functions. Name block and function comments are required as usual.Explanation / Answer
#include <iostream>
using namespace std;
int main() {
std:: vector <int> v;
int n,x,ch;
int * a= NULL;
a = new int[10];
cout<< “ Enter no of elements”;
cin>> n;
if(n>=20) {
cout << “Error”<<end1;
}else
cout << “Continue”;
while (1) {
cout << Enter choice”;
cin >>ch;
switch(ch) {
cout << “MENU”;
case 1: cout << “Output all values to the screen”;
break;
case 2: cout <<”Add new data value”;
add();
break;
case 3 : cout<< “Remove data value”;
remove();
break;
case 4: cout < “ sort the data”;
sort();
break;
case 5: cout <<” Quit”;
break;
case default:
cout<< “Invalid choice” << ch;
}
}
add() {
cout << “Enter value”;
cin>>x;
int m = find(a[],x);
if(m!=-1)
cout << “Element exists”;
else cout <<”Element doesn’t exist”;
for( int i=0;i<=n;i++) {
a[i] = x; }
l = find(v.begin(),v.end(),x);
if(l!=v.end())
cout<<”Element exists”;
else cout<< “Element doesn’t exist;
for(int i=0;i<=n;i++) {
int b{} = v.push_back(x); }
cout << “The array elements:” << a[i];
cout<<”The vector elements: << b{};
}
remove() {
cout << “Enter value”;
cin>>x;
l = find(v.begin(),v.end(),x);
if(l!=v.end())
cout<<”Element exists”;
else cout<< “Element doesn’t exist;
while(!v.empty()) {
c = v.pop_back(x);
int s = b{} – c;
}
cout<< “The elements in vector:” << s;
cout << “Enter value”;
cin>>x;
int m = find(a[],x);
if(m!=-1)
cout << “Element exists”;
else cout <<”Element doesn’t exist”;
while(a[]!=-1) {
delete(x);
int s[] = a[]-x;}
cout << “ The elements in array:”<<s; }
sort() {
cout<< “ Enter value”;
cin>>x;
std:: sort(v.begin(),v.end(),x);
cout<<”sorted vector:”;
for(int i= v.begin() ; i!=v.end();i++) {
cout<<’ ‘<<i; }
sort(a,a+n);
cout <<”Sorted array:”;
for(int i=0;i!=n;i++) {
cout << a[i]; }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.