Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete C++ program that will: Declare a vector of integers Use a point

ID: 3681323 • Letter: W

Question

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>

#include <vector>
#include <algorithm>  
bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;
static int n;
using namespace std;

int main()
{
   // create a vector to store int
   vector<int> vec;
   int i,k,c=0;

cout<<"Enter number of elements : ";
cin>>n;
int *a=new int[20];
   // push 5 values into the vector
   for(i = 0; i < n; i++){
    cin>>k;
       a[i]=k;
      vec.push_back(k);
   }

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;



   while(c!=1)
   {
    cout<<"1.Display 2.Add 3.Delete 4.Sort 5.Exit";
    cin>>k;
    switch(k)
    {
        case 1:
           for(i = 0; i < n; i++){
            cout<<" a["<<i<<"] = "<<a[i]<<endl;
           cout << "value of vec [" << i << "] = " << vec[i] << endl;
       }
           break;
        case 2:
           int val,flag;
           flag=0;
           cout<<"enter value you want to add : ";
           cin>>val;
           for(i=0;i<n;i++)
           {
               if(a[i]==val)
               flag=1;
           }
           if(flag!=1 && i!=21)
           {
               a[i]=val;
               vec.push_back(val);
               n++;
           }
           break;
       case 3:
           int in;
           cout<<"Enter value you want to remove: ";
           cin>>val;
           for(i=0;i<n;i++)
           {
               if(a[i]==val)
               in=i;
           }
           if(in!=n-1)
           a[in]=a[in+1];
           else
           {
           n--;
           break;
            }
              
            vec.erase (vec.begin()+(in));
      
           for(i=in+1;i<n-1;i++)
           {
              
               a[i]=a[i+1];
           }
          
            n--;
           break;
       case 4:
               // set flag to 1 to start first pass
            int temp;             // holding variable
            int j;
            for(i = 0; i < n-1; i++)
            {
            
               for (j=i; j < n ; j++)
               {
                  if (a[j]< a[i])      // ascending order simply changes to <
                  {
                    temp = a[i];             // swap elements
                    a[i] = a[j];
                    a[j] = temp;
                         
                  }
              }
            }
             // using default comparison (operator <):
          std::sort (vec.begin(), vec.begin()+4);           //(12 32 45 71)26 80 53 33

// using function as comp
           std::sort (vec.begin()+4, vec.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
            std::sort (vec.begin(), vec.end(), myobject);
            break;
        case 5:
           c=1;
           break;
      }
}
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote