Please i need this done in C++ language that compiles and works properly. Also p
ID: 3910313 • Letter: P
Question
Please i need this done in C++ language that compiles and works properly. Also please don't use vectors.
The Magic Bag The ma gic bag data type will be implemented as follows Objects can be inserted into the magic bag using the MagicBag::insert(item) member function. The magic bag can hold any number of items, within the bounds of the available RAM. Duplicate items are allowed Objects are removed from the magic bag using the MagicBag::draw() member function. This function returns a random item and removes it from the bag. The returned item should be randomized In other words, if there are 5 items in the bag, and only one of them is a 7, then there should be a 1 in 5 chance of drawing a 7 with the draw) function. If the bag is empty, the drawo function should throw an exception. .You can check if an object is in the bag using MagicBag: :peek (item), which should return 0 if there is no item in the bag matching the item parameter. If there are items matching the item parameter in the bag, the number (count) of matching items should be returned .You can print a magic bag using the MagicBag: :print (ostream&) member function. This function should print to any ostream you pass to it (cout, cerr, etc.). You should be able to assign the contents of a MagicBag using the operator. This should result in a copy of the magic bag that is not linked to the original bag. In other words, if a and b are magic bags the line "a = b;" should result in bags a and b having the same contents. If I then draw elements from bag b, this should not change the contents of bag a .You should begin by constructing a magic bag that holds integers. For additional credit, you can create a template so that the magic bag can hold any primitive data type (see below for grading details). You can implement the magic bag using an array, a linked, list, or any other data structure that you feel is appropriate. You need to design and implement the underlying data structure yourself. You may not for example, use C++ standard template library vectors for the underlying data structure You will be provided with a main program for testing your magic bag No points will be awarded for submissions that do not compile No points will be awarded for line items that produce a segmentation fault. .Explanation / Answer
Given below is the code for the question using dynamic arrays. It implements the bag as a template (extra credit), so it can be used with any data type.
You can test it with the test program given by instructor. In case of any issues, please let me know , I'll help.
Please do rate the answer if it was helpful. Thank you
MagicBag.h
==========
#ifndef MAGIC_BAG
#define MAGIC_BAG
#include <cstdlib>
#include <exception>
#include <iostream>
using namespace std;
template <typename T>
class MagicBag {
private:
T* data;
int size;
int capacity;
public:
MagicBag(int cap = 10);
MagicBag(const MagicBag &other);
void insert(T item);
T draw();
int peek(T item);
void print(ostream &out);
MagicBag& operator =(const MagicBag &other);
~MagicBag();
};
template <typename T>
MagicBag<T>::MagicBag(int cap)
{
capacity = cap;
data = new T[capacity];
size = 0;
}
template <typename T>
MagicBag<T>::MagicBag(const MagicBag &other)
{
data = NULL;
*this = other;
}
template <typename T>
void MagicBag<T>::insert(T item)
{
if(size == capacity)
{
capacity *= 2;
T* temp = new T[capacity];
for(int i = 0; i < size; i++)
temp[i] = data[i];
delete []data;
data = temp;
}
data[size++] = item;
}
template <typename T>
T MagicBag<T>::draw()
{
if(size == 0)
throw runtime_error("bag empty. can't draw!");
int index = rand() % size;
T val = data[index];
data[index] = data[size-1]; //store the last item in bag into the deleted index
size--;
return val;
}
template <typename T>
int MagicBag<T>::peek(T item)
{
int count;
for(int i = 0; i < size; i++)
{
if(data[i] == item)
count++;
}
return count;
}
template <typename T>
void MagicBag<T>::print(ostream &out)
{
for(int i = 0; i < size; i++)
out << data[i] << " ";
out << endl;
}
template <typename T>
MagicBag<T>& MagicBag<T>::operator=(const MagicBag<T> &other)
{
if(data != NULL)
delete []data;
capacity = other.capacity;
size = other.size;
data = new T[capacity];
for(int i =0 ;i < size; i++)
data[i] = other.data[i];
return *this;
}
template <typename T>
MagicBag<T>::~MagicBag()
{
delete []data;
}
#endif