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

A bag can contain more than one copy of an item. For example, the chapter descri

ID: 667964 • Letter: A

Question

A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two copies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given item. Write a new container class called set, which is similar to a bag, except that a set can contain only one copy of any given item. You’ll need to change the interface a bit. For example, in-
stead of the bag’s count function, you’ll want a constant member function such as this: bool set::contains (const value_type& target) const;
// Postcondition: The return value is true if
// target is in the set; otherwise the return
// value is false.
Make an explicit statement of the invariant of the set class. Do a time analysis for each operation. At this point, an efficient implementation is not needed. For example, just adding a new item to a set will take linear time because you’ll need to check that the new item isn’t already present. Later we’ll explore more efficient implementations (including the implementation of set in the C++ Standard Library). You may also want to add additional operations to your set class, such as an operator for subtraction.

Explanation / Answer

This will work :

try this :
The Bag Class Abstraction.
This bag will be our example of a container class. This is a class where each object can contain a collection of items (such as numbers). One of the important facets of a container class is that each object begins in a known configuration. In the case of a bag, we will count on each bag being initially empty. This is called the initial state of a bag.

We will use a fixed collection of operations to manipulate bags.
Insert a number in the bag.
One of the simplest operations that we permit is the action of inserting a new number into a bag.
Query a bag contents
We will have two Query methods. Is a number in the bag? How many copies of a number is in the bag?
Remove a number
The next operation is to remove a single number from the bag.
Remove all members from the bag.
Clear the bag.
Another operation allows us to find out how many numbers are in the bag, what is the size of the bag?

Bag Class implemetation.
The header file
//
#ifndef _BAG_
#define _BAG_

typedef int bag_type;

class Bag {

private:
   int       count;     // members in bag
   bag_type data[20]; // data store up to 20 members

public:
           Bag();             // Constructor
   bool    insert(bag_type); // Put a member in the bag
   bool    remove(bag_type); // remove a member from the bag
   int     size();            // number of members in bag
   void    clear();           // remove all members from bag
   bool    inbag(bag_type);   // is a member in the bag?
   int     howmany(bag_type); // how many member in bag.
};
#endif
The Bag implementation Code
#include "bag.h"
#include <iostream>

Bag::Bag()    // Constructor
{
   // set the initial state of the bag
   count=0;
}

void Bag::clear() // Clear the bag
{
   count=0;
}

bool Bag::insert(bag_type value) //Place a value in Bag
{
   bool reply;
   if(count <20 ) {
      data[count]=value;
      reply=true;
      count++;
   } else {
      reply=false;
   }
   return reply;
}

bool Bag::inbag(bag_type value) // Is a value in the bag?
{
   bool reply=false;
   int index;
   for(index=0;index<count&& !reply;index++)
      if(data[index] == value) reply=true;
   return reply;
}

int Bag::howmany(bag_type value) //How many of element;
{
   int thismany=0;
   int index;
   for(index=0;index<count;index++)
      if(data[index]==value) thismany++;
   return thismany;
}

bool Bag::remove(bag_type value) // Remove a value
{
   bool reply=false;
   int index;
   if(howmany(value) == 0) return reply;
   reply=true;
   index=0;
   while(data[index] != value) index++;
   for(;index<count;index++)
      data[index]=data[index+1];
   count--;
}

int Bag::size()    // How many elements in bag?
{
   return count;
}
Driver to test the Bag
#include <iostream>
#include <cstdlib>
#include "bag.h"

using namespace std;

int main(int argc, char *argv[])
{
Bag b;
bag_type value;

cout << "Bag" << endl;
b.insert(4);
do {
     value=rand()%6 +1;
} while(b.insert(value));
cout << b.size()<< " elements in the bag" << endl;
cout << b.howmany(4) << " fours " << endl;
b.remove(4);
cout << b.size()<< " elements in the bag" << endl;
cout << b.howmany(4) << " fours " << endl;
cout << b.howmany(5) << " fives " << endl;
while(b.inbag(5)) b.remove(5);
cout << b.howmany(5) << " fives " << endl;

system("Pause");
return 0;
}
Test Run
Bag
20 elements in the bag
3 fours
19 elements in the bag
2 fours
4 fives
0 fives
Press any key to continue . . .

Also summary of this bag containter class :

Container Classes

Bags

Initial State of a Bag

Inserting Numbers into a Bag

Examining a Bag

Removing a Number from a Bag

How Many Numbers

Summary of the Bag Operations

The Bag Class

The Bags Default Constructor

The Insert Function

The Size Function

The Occurrences Function

The Remove Function

Using the Bag in a Program

// Record the ages of three children:
ages.insert(4);
ages.insert(8);
ages.insert(4);

The Header File and Implementation File

Documentation for the Bag Class

The Bags Class Definition

The Implementation File

A Quiz
Suppose that a Mysterious Benefactor provides you with the Bag class, but you are only permitted to read the documentation in the header file. You cannot read the class definition or implementation file. Can you write a program that uses the Bag data type ?

Answer
Suppose that a Mysterious Benefactor provides you with the Bag class, but you are only permitted to read the documentation in the header file. You cannot read the class definition or implementation file. Can you write a program that uses the Bag data type ?

You know the name of the new data type, which is enough for you to declare Bag variables. You also know the headings and specifications of each of the operations.

Implementation Details

4

8

4

?

?

?

4

4

8

?

?

?

8

4

4

?

?

?

3


4

8

4

?

?

?

An Exercise

Use these ideas to write a list of private member variables could implement the Bag class. You should have two member variables. Make the bag capable of holding up to 20 integers.









An Example of Calling Insert

  


Pseudocode for Bag::insert

The Other Bag Operations

Other Kinds of Bags

Try this as well :

4

8

4

?

?

?

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