Question

Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h). The RemoveRandom member f

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE CODE FOR THE ABOVE QUESTION IS :

#include <vector>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

template <typename T>
class ArrayBag
{
   private:
   vector<T> bag;
  
   public:
   // constructor
   ArrayBag(vector<T> arrayBag)
   {
       this->bag = arrayBag;
   }// end constructor
  
   // remove an item randomly
   void RemoveRandom(int index_to_remove)
   {
       T removed = bag.at(index_to_remove);
       bag.erase((bag.begin() + index_to_remove));
       cout << "Removed Element: " << removed << endl;
   }// end RemoveRandom
  
   // function to print the vector
   void printBag()
   {
       int size = this->bag.size();
       for(int i = 0; i < size ; i++)
           cout << this->bag.at(i) << " ";
       cout << endl;
   }// end printBag
};

int main()
{
   int n = 5;
   // creating vectors
   vector<int> v1 = {1,2,3,4,5};
   vector<float> v2 = {1.1,2.2,3.3,4.4,5.5};
   vector<string> v3 = {"Item1","Item2","Item3","Item4","Item5"};
  
   // creating array bag objects and initialising using constructor
   ArrayBag<int> a1(v1);
   ArrayBag<float> a2(v2);
   ArrayBag<string> a3(v3);
  
   srand(time(0));
   // testing out prototype
   cout << "ArrayBag 1: " ;
   a1.printBag();
   cout << "Removing an item randomly. " ;
   a1.RemoveRandom(rand() % n);
   cout << "ArrayBag 1: " ;
   a1.printBag();
   cout<<endl;
  
  
   cout << "ArrayBag 2: " ;
   a2.printBag();
   cout << "Removing an item randomly. " ;
   a2.RemoveRandom(rand() % n);
   cout << "ArrayBag 2: " ;
   a2.printBag();
   cout<<endl;


   cout << "ArrayBag 3: " ;
   a3.printBag();
   cout << "Removing an item randomly. " ;
   a3.RemoveRandom(rand() % n);
   cout << "ArrayBag 3: " ;
   a3.printBag();

   return 0;
}

SAMPLE OUTPUT:

ArrayBag 1: 1 2 3 45 Removing an item randomly. Removed Element: 5 ArrayBag 1 1 2 3 4 ArrayBag 2: 1.1 2.2 3.3 4.4 5.5 Removin

Add a comment
Know the answer?
Add Answer to:
Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because ...

    This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because we do some thing like this on exams. All you will need to submit is your member function code BUT your code should compile and run if I were to add it to ArrayBag.h. That means you should put your code in ArrayBag.h and make sure it compiles before submitting. You should test with a simple main. DO NOT submit main or your ArrayBag.h file just submit...

  • you will write a templated array class. Called MyArray with member variables ptr and array_size. This...

    you will write a templated array class. Called MyArray with member variables ptr and array_size. This array must use dynamic memory--see program shell, names should remain unchanged. The following is a list of required member functions of the array class along with a rubric: (1pts) program compiles (2pts) default constructor (2pts) parametered constructor which feeds MyArray a size. (10pts) copy constructor 8pts for performing a *deep* copy 2pts for correct syntax (10pts) overloaded = operator 7pts for functioning properly 3pts...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name.  Add a constructor which expects color and  name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables Color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name. Add a constructor which expects color and name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of...

  • in C++ creat a DynamicQueue class, add a new data member called count to trace the...

    in C++ creat a DynamicQueue class, add a new data member called count to trace the total number of node you have in current queue (you need to modify some member functions for adding count). Add a member function called displayQueue() to display values stored in each node in the current queue, also the total number of nodes in the queue. You also need to have a driver program (refer to Tester) to test your modified new class and new...

  • Write a class called Book that has two private member variables called page (integer) and topic...

    Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say: book2 = book1 + 4; then...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • Add a public member function to the BST class below that returns the size of the...

    Add a public member function to the BST class below that returns the size of the tree—the number of the nodes in the tree. For simplicity, the class contains only the key and not a value. You may add any helper functions you find necessary. (Hint: think recursion) template <typename T> struct Node {                 T key;                 Node<T>*              left;                 Node<T>*              parent;                 Node<T>*              right; }; template <typename T> class BST { private:         Node<T>* root; public:         BST():...

  • (COSC28-SLO2+SLO8] Given the portion of the class definition below, add a member function called print that...

    (COSC28-SLO2+SLO8] Given the portion of the class definition below, add a member function called print that prints the member variable report and flog. class Log { string report = ""; string flag = ""; public: // constructor Log(string flag) { this->flag = flag; this->report = "Report logged for " + flag; 3 // The print function to be added here 3 I A - IX E 3 7 1 x X, DE - V GT 12pt Paragraph v

  • C++ / Visual Studio Implement the member function below that returns true if the given value...

    C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT