Define a class Bag. Bag has only difference from Set that it may contain same item multiple times. Data members are same as of set class. Add following functions: - Bag();//take array of size 5 and initialize elements randomly, - Bag(int size);//take array of given size, set size to given size and cSize to 0 - Bag(int *d, const int SIZE); //take array of given size, set cSize to 0, call add function in loop - Bag(const Bag &b);//Set size & cSize same as of b. Take array of size and copy elements from b - void show();//print elements in single line separated by spaces - void add(int);//add element, if cSize
/*As the requirement described by you, please find the code
below*/
#include<iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
/*Class Bag, can store same item multiple times*/
class Bag
{
private:
int array[100];
int size;
int cSize;
public:
//take array of size 5 and
initialize elements randomly
Bag()
{
size=5;
cSize=0;
for(int
i=0;i<size;i++)
{
array[i]=rand();
}
}
//take array of given size, set
size to given size and cSize to 0
Bag(int size)
{
this->size=size;
cSize=0;
for(int
i=0;i<size;i++)
{
array[i]=rand();
}
}
//take array of given size, set
cSize to 0, call add function in loop
Bag(int*d,const int size)
{
this->size=size;
cSize=0;
for(int
i=cSize;i<size;i++)
{
add(i);
}
}
//Set size & cSize same as of
b. Take array of size and copy elements from b
Bag(Bag *b)
{
size=b->getSize();
cSize=b->getCSize();
for(int
i=cSize;i<size;i++)
{
array[i]=b->getArray()[i];
}
}
//function to get private data
member.
int getSize()
{
return
size;
}
int getCSize()
{
return
cSize;
}
int* getArray()
{
return
array;
}
//print elements in single line
separated by spaces
void show()
{
for(int
i=cSize;i<size;i++)
{
cout<<" "<<array[i];
}
cout<<endl;
}
//add element at position in
index
void add(int index)
{
cin>>array[index];
}
};
class Set
{
private:
int array[100];
int size;
int cSize;
public:
Set()
{
size=5;
cSize=0;
for(int
i=0;i<size;i++)
{
array[i]=rand();
}
}
Set(int size)
{
this->size=size;
cSize=0;
for(int
i=0;i<size;i++)
{
array[i]=rand();
}
}
Set(int*d,const int size)
{
this->size=size;
cSize=0;
for(int
i=cSize;i<size;i++)
{
if(!add(i))
{
cout<<"\nAlready exist.
Insert again\n";
i--;
}
}
}
Set(Set *b)
{
size=b->getSize();
cSize=b->getCSize();
for(int
i=cSize;i<size;i++)
array[i]=b->getArray()[i];
}
int getSize()
{
return
size;
}
int getCSize()
{
return
cSize;
}
void show()
{
for(int
i=cSize;i<size;i++)
{
cout<<" "<<array[i];
}
cout<<endl;
}
int* getArray()
{
return
array;
}
int add(int index)
{
cin>>array[index];
for(int
i=cSize;i<=index-1;i++)
{
if(array[i]==array[index])
{
return 0;
}
}
return 1;
}
};
int main()
{
int a=0;
//calls for Bag class
srand(time(NULL));
Bag bag_obj1;
bag_obj1.show();
Bag bag_obj2(7);
bag_obj2.show();
Bag bag_obj3(&a,8);
bag_obj3.show();
Bag bag_obj_copy(bag_obj3);
bag_obj_copy.show();
//calls for Set class
Set Set_obj1;
Set_obj1.show();
Set Set_obj2(7);
Set_obj2.show();
Set Set_obj3(&a,8);
Set_obj3.show();
Set Set_obj_copy(Set_obj3);
Set_obj_copy.show();
return 0;
}
/*Feel free to ask questions in comments*/
Define a class Bag. Bag has only difference from Set that it may contain same item...
5. Below i s the class deciaration for the Bag class from your text. Refer to this hea he Ted implementation @file Bag.h #ifndet BAG #define BAG template <class ItemType> class Bag private: static const int DEFAULT BAG SIZE 6; Il current count of Bag items /I max capacity of the Bag ItemType items[DEFAULT BAG SIZE]; //array of Bag items int itemCount; int maxitems; /l Returns either the index of the element in the array items that ll contains the...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...
/ Animal.hpp
#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <string>
class Animal
{
public:
Animal();
Animal(std::string, bool domestic=false, bool
predator=false);
std::string getName() const;
bool isDomestic() const;
bool isPredator() const;
void setName(std::string);
void setDomestic();
void setPredator();
protected: // protected so that derived class can directly
access them
std::string name_;
bool domestic_;
bool predator_;
};
#endif /* ANIMAL_H_ */
//end of Animal.h
//
/////////////////////////////////////////////////////////////////Animal.cpp
#include "Animal.h"
Animal::Animal(): name_(""),domestic_(false),
predator_(false){
}
Animal::Animal(std::string name, bool domestic, bool
predator):
name_(name),domestic_(domestic), predator_(predator)
{
}
std::string Animal::getName() const{
return...
Can someone finish this class for me? There are comment instructions with what to do in the methods already. public class ArrayDemo { public void demonstrateTask1(){ System.out.println("Demonstrate Task 1"); int[] numbers = null; // note that numbers references nothing (null) initially ...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...
C++ Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dynamic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting of words separated...
PLEASE HURRY. Below is the prompt for this problem. Use the code for bag1.cxx, bag1.h and my code for bag.cpp. Also I have provided errors from linux for bag.cpp. Please use that code and fix my errors please. Thank you The goal of assignment 3 is to reinforce implementation of container class concepts in C++. Specifically, the assignment is to do problem 3.5 on page 149 of the text. You need to implement the set operations union, intersection, and relative...
Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the source code (shown below) to output the following: Inserting elements to array list: The list contains 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Deleting elements: The list contains: 2 4 6 8 10 12 14 16 18 20 22 24 this is a programming problem in...
Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation. Hi, I wrote the program but I don"t know why it showing the output - 000 000 0 0 My main.cpp file code is this - #include<stdio.h> #include<iostream> #include<fstream> #include "ArraySet.h" #include "ArraySet.cpp" using namespace std; int main(){ ArraySet<int> setA, setB, setC; // adds to setA setA.add(10); setA.add(20); setA.add(30); //...