Question

Implement operators for - and -= for the bag class. For two bags x and y,...

Implement operators for - and -= for the bag class. For two bags x and y, the bag x-y contains all the items of x, with any items from y removed. For example,
suppose that x has seven copies of the number 3, and y has two copies of the number 3. Then x-y will have five copies of the number 3 (i.e., 7 - 2 copies of the number 3). In the case where y has more copies of an item than x does, the bag x-y will have no copies of that item. For example, suppose that x has nine copies of the number 8, and y has 10
copies of the number 8. Then x-y will have no 8s. The statement x -= y should have the same effect as the assignment x = x-y;

I tried one program same from Chegg solutions but it didn't work can you please help me? debug so it will work the complete program in visual basic use c++ language.


only one bag class
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE THUMBS UP, THANKS

Sample output:

C:\Users\VISHAL\Documents\Bag_class.exe Bag A 8 8 8 8 8 8 8 8 8 8 Bag B 8 8 8 8 8 8 8 Print A-A-B : 8 8 8 Process exited afte

code:

#include<iostream>
#include<vector>
using namespace std;

class Bag
{
   private:
       vector<int> List;
   public:
       Bag()
       {
           List=vector<int>();
       }
       void insert(int data)
       {
           List.push_back(data);
       }
       int getSize(){
           return List.size();
       }
       int getElementAt(int i)
       {
           return List.at(i);
       }
       void setElementAt(int i,int data)
       {
           List.at(i)=data;
       }
       void deleteElementAt(int index)
       {
           List.erase(List.begin()+index);
       }
       Bag operator -(Bag &B)
       {
           Bag temp1=Bag();
           Bag temp2=Bag();
           for(int i=0; i<this->getSize();i++)
           {
               temp1.insert(this->getElementAt(i));
           }
           for(int i=0; i<B.getSize();i++)
           {
               temp2.insert(B.getElementAt(i));
          
           }
           for(int i=temp2.getSize()-1;i>=0; i--)
           {
               for(int j=temp1.getSize()-1; j>=0; j--)
               {
                   if(temp1.getElementAt(j)==temp2.getElementAt(i))
                   {
                       temp1.deleteElementAt(j);
                       temp2.getElementAt(i);
                       break;
                   }
               }
           }
           return temp1;
       }
       void print()
       {
           for(int i=0; i<List.size();i++)
           {
               cout<<List.at(i)<<" ";
           }
           cout<<endl;
       }
};
int main()
{
   Bag A=Bag();
   Bag B=Bag();
   for(int i=1; i<=10; i++)
   {
       A.insert(8);
   }
   cout<<"Bag A : ";A.print();
   for(int i=1; i<=7; i++)
   {
       B.insert(8);
   }
   cout<<"Bag B : ";B.print();
   A=A-B;
   cout<<"Print A=A-B : ";A.print();
  
}

Add a comment
Know the answer?
Add Answer to:
Implement operators for - and -= for the bag class. For two bags x and y,...
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
  • computer science c++ 6. The union of two bags is a new bag containing the combined...

    computer science c++ 6. The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method’s one argument. Include suffi cient comments to fully specify the method. Note that the union of two bags might contain duplicate items. For example,...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • How to solve this Problem in C++ . The Problem Write program that uses a class...

    How to solve this Problem in C++ . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the set...

  • The ADT Bag is a group of items, much like what you might have with a...

    The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document. ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should be included in an interface.) Specify operations to  create an empty bag that can hold up to 100...

  • Consider the trash bag problem. Suppose that an independent laboratory has tested trash bags and has...

    Consider the trash bag problem. Suppose that an independent laboratory has tested trash bags and has found that no 30-gallon bags that are currently on the market have a mean breaking strength of 50 pounds or more. On the basis of these results, the producer of the new, improved trash bag feels sure that its 30-gallon bag will be the strongest such bag on the market if the new trash bag’s mean breaking strength can be shown to be at...

  • Multiple Choice Multiple Choice Section 3.1 The Bag ADT For the bag class in Chapter 3...

    Multiple Choice Multiple Choice Section 3.1 The Bag ADT For the bag class in Chapter 3 (using a fixed array and a typedef statement) what steps were necessary for changing from a bag of integers to a bag of double values? A. Change the array declaration from int data[CAPACITY] to double data[CAPACITY] and recompile. B. Change the int to double in the typedef statement and recompile. C. Round each double value to an integer before putting it in the bag....

  • what would be the solution code to this problem in c++? The Problem Write program that...

    what would be the solution code to this problem in c++? The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of...

  • PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is,...

    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...

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