PLEASE ATTACH SCREEN SHOT OF RESULTS. // FILE: bag_demo.cxx // This is a small d
ID: 3842315 • Letter: P
Question
PLEASE ATTACH SCREEN SHOT OF RESULTS.
// FILE: bag_demo.cxx
// This is a small demonstration program showing how the bag class is used.
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "bag1.h" // With value_type defined as an int
using namespace std;
//using namespace main_savitch_3;
// PROTOTYPES for functions used by this demonstration program:
void get_ages(bag& ages);
// Postcondition: The user has been prompted to type in the ages of family
// members. These ages have been read and placed in the ages bag, stopping
// when the bag is full or when the user types a negative number.
void check_ages(bag& ages);
// Postcondition: The user has been prompted to type in the ages of family
// members once again. Each age is removed from the ages bag when it is typed,
// stopping when the bag is empty.
int main( )
{
bag ages;
get_ages(ages);
check_ages(ages);
cout << "May your family live long and prosper." << endl;
return EXIT_SUCCESS;
}
void get_ages(bag& ages)
{
int user_input;
cout << "Type the ages in your family." << endl;
cout << "Type a negative number when you are done:" << endl;
cin >> user_input;
while (user_input >= 0)
{
if (ages.size( ) < ages.CAPACITY)
ages.insert(user_input);
else
cout << "I have run out of room and cant add that age." << endl;
cin >> user_input;
}
}
void check_ages(bag& ages)
{
int user_input;
cout << "Type those ages again. Press return after each age:" << endl;
while (ages.size( ) > 0)
{
cin >> user_input;
if (ages.erase_one(user_input))
cout << "Yes, I've got that age and will remove it." << endl;
else
cout << "No, that age does not occur!" << endl;
}
}
// FILE: bag1.cxx
// CLASS IMPLEMENTED: bag (see bag1.h for documentation)
// INVARIANT for the bag class:
// 1. The number of items in the bag is in the member variable used;
// 2. For an empty bag, we do not care what is stored in any of data; for a
// non-empty bag the items in the bag are stored in data[0] through
// data[used-1], and we don't care what's in the rest of data.
#include <algorithm> // Provides copy function
#include <cassert> // Provides assert function
#include "bag1.h"
using namespace std;
//namespace main_savitch_3
//{
const bag::size_type bag::CAPACITY;
bag::size_type bag::erase(const value_type& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used)
{
if (data[index] == target)
{
--used;
data[index] = data[used];
++many_removed;
}
else
++index;
}
return many_removed;
}
bool bag::erase_one(const value_type& target)
{
size_type index; // The location of target in the data array
// First, set index to the location of target in the data array,
// which could be as small as 0 or as large as used-1. If target is not
// in the array, then index will be set equal to used.
index = 0;
while ((index < used) && (data[index] != target))
++index;
if (index == used)
return false; // target isnt in the bag, so no work to do.
// When execution reaches here, target is in the bag at data[index].
// So, reduce used by 1 and copy the last item onto data[index].
--used;
data[index] = data[used];
return true;
}
void bag::insert(const value_type& entry)
// Library facilities used: cassert
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
void bag::operator +=(const bag& addend)
// Library facilities used: algorithm, cassert
{
assert(size( ) + addend.size( ) <= CAPACITY);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
bag::size_type bag::count(const value_type& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i])
++answer;
return answer;
}
bag operator +(const bag& b1, const bag& b2)
// Library facilities used: cassert
{
bag answer;
assert(b1.size( ) + b2.size( ) <= bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
//}
// FILE: bag1.h
// CLASS PROVIDED: bag (part of the namespace main_savitch_3)
//
// TYPEDEF and MEMBER CONSTANTS for the bag class:
// typedef ____ value_type
// bag::value_type is the data type of the items in the bag. It may be any of
// the C++ built-in types (int, char, etc.), or a class with a default
// constructor, an assignment operator, and operators to
// test for equality (x == y) and non-equality (x != y).
//
// typedef ____ size_type
// bag::size_type is the data type of any variable that keeps track of how many items
// are in a bag.
//
// static const size_type CAPACITY = _____
// bag::CAPACITY is the maximum number of items that a bag can hold.
//
// CONSTRUCTOR for the bag class:
// bag( )
// Postcondition: The bag has been initialized as an empty bag.
//
// MODIFICATION MEMBER FUNCTIONS for the bag class:
// size_type erase(const value_type& target);
// Postcondition: All copies of target have been removed from the bag.
// The return value is the number of copies removed (which could be zero).
//
// void erase_one(const value_type& target)
// Postcondition: If target was in the bag, then one copy has been removed;
// otherwise the bag is unchanged. A true return value indicates that one
// copy was removed; false indicates that nothing was removed.
//
// void insert(const value_type& entry)
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of entry has been added to the bag.
//
// void operator +=(const bag& addend)
// Precondition: size( ) + addend.size( ) <= CAPACITY.
// Postcondition: Each item in addend has been added to this bag.
//
// CONSTANT MEMBER FUNCTIONS for the bag class:
// size_type size( ) const
// Postcondition: The return value is the total number of items in the bag.
//
// size_type count(const value_type& target) const
// Postcondition: The return value is number of times target is in the bag.
//
// NONMEMBER FUNCTIONS for the bag class:
// bag operator +(const bag& b1, const bag& b2)
// Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY.
// Postcondition: The bag returned is the union of b1 and b2.
//
// VALUE SEMANTICS for the bag class:
// Assignments and the copy constructor may be used with bag objects.
#ifndef MAIN_SAVITCH_BAG1_H
#define MAIN_SAVITCH_BAG1_H
#include <cstdlib> // Provides size_t
//namespace main_savitch_3
//{
class bag
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
bag( ) { used = 0; }
// MODIFICATION MEMBER FUNCTIONS
size_type erase(const value_type& target);
bool erase_one(const value_type& target);
void insert(const value_type& entry);
void operator +=(const bag& addend);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return used; }
size_type count(const value_type& target) const;
private:
value_type data[CAPACITY]; // The array to store items
size_type used; // How much of array is used
};
// NONMEMBER FUNCTIONS for the bag class
bag operator +(const bag& b1, const bag& b2);
//}
#endif
Bonus 6 (20 points) A. Get the "bag1" code to run in your environment. B. Expand bag1 demo to test any member functions that have not been tested.Explanation / Answer
int main( )
{
bag ages;
get_ages(ages);
check_ages(ages);
cout << "May your family live long and prosper." << endl;
return EXIT_SUCCESS;
}
void get_ages(bag& ages)
{
int user_input;
cout << "Type the ages in your family." << endl;
cout << "Type a negative number when you are done:" << endl;
cin >> user_input;
while (user_input >= 0)
{
if (ages.size( ) < ages.CAPACITY)
ages.insert(user_input);
else
cout << "I have run out of room and cant add that age." << endl;
cin >> user_input;
}
}
void check_ages(bag& ages)
{
int user_input;
cout << "Type those ages again. Press return after each age:" << endl;
while (ages.size( ) > 0)
{
cin >> user_input;
if (ages.erase_one(user_input))
cout << "Yes, I've got that age and will remove it." << endl;
else
cout << "No, that age does not occur!" << endl;
}
}
void bag::operator +=(const bag& addend)
// Library facilities used: algorithm, cassert
{
assert(size( ) + addend.size( ) <= CAPACITY);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.