Question
Array Bag Driver. ayyarBagdriver.cpp #include #include #include ArrayBag.h using namespace std; void display(ArrayBag & bag) { cout vector bagItems = bag.toVector(); int numberOfEntries = static_cast
Array Bag Driver.
ayyarBagdriver.cpp
#include
#include
#include "ArrayBag.h"
using namespace std;
void display(ArrayBag
{
cout
vector
int numberOfEntries = static_cast
for (int i = 0; i
{
cout
} // end for
cout
} // end displayBag
int main()
{
ArrayBag
cout
cout
cout
display(bag);
string items[] = {"one", "two", "three", "four", "one"};
cout
for (int i = 0; i
{
bag.add(items[i]);
} // end for
display(bag);
cout
cout
cout
cout
cout
cout
display(bag);
cout
bag.clear();
cout
return 0;
} // end main
BagInterface.h
/** Listing 1-1.
@file BagInterface.h */
#ifndef BAG_INTERFACE_
#define BAG_INTERFACE_
#include
template
class BagInterface
{
public:
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtual int getCurrentSize() const = 0;
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this bag.
@post If successful, newEntry is stored in the bag and
the count of items in the bag has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag,
if possible.
@post If successful, anEntry has been removed from the bag
and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this bag.
@post Bag contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that
are in this bag.
@return A vector containing all the entries in the bag. */
virtual std::vector
/** Destroys object and frees memory allocated by object.
(See C++ Interlude 2) */
virtual ~BagInterface () { }
}; // end BagInterface
#endif
Array.h
/** Header file for an array-based implementation of the ADT bag.
@file ArrayBag.h */
#ifndef ARRAY_BAG_
#define ARRAY_BAG_
#include "BagInterface.h"
template
class ArrayBag : public BagInterface
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
std::vector
}; // end ArrayBag
// Created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
/** Implementation file for the class ArrayBag.
@file ArrayBag.cpp */
#include
template
ArrayBag
{
} // end default constructor
template
int ArrayBag
{
return itemCount;
} // end getCurrentSize
template
bool ArrayBag
{
return itemCount == 0;
} // end isEmpty
template
bool ArrayBag
{
bool hasRoomToAdd = (itemCount
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
template
bool ArrayBag
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
template
void ArrayBag
{
itemCount = 0;
} // end clear
template
int ArrayBag
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template
bool ArrayBag
{
return getIndexOf(anEntry) > -1;
} // end contains
template
std::vector
{
std::vector
for (int i = 0; i
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template
int ArrayBag
{
bool found = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
#endif
C++ (Object Oriented Programming) Recursion & Set ADT. The programming is divided in to two parts. First, we need a function that processes the elements of an array using recursion. Then, we implement a set class template using an array. Requirements: -Use of Binary search Find the largest element in an array using the recursive technique Each call to the recursive binary search function processes a portion of the array identified by the first and last indexes of the subarray of the subarray to be processes by this call. 1) First, we need a function called maxArray that will return the largest value in an integer array. A driver to test the function. The maxArray function and driver program can be in the same file Algorithm: Use the given algorithm to find the largest value in the array. if (anArray has only one entry) return the value of that entry else if (anArray has more than one entry) return the maximum of maxArray(left half of anArray) and maxArray(right half of anArray) BinarySearch function always returns value. For this program, your function will never be called with first > last. If familiar with the assert statement, you can use it to handle this error. Objectives A set is like a bag, except that duplicate entries are not allowed in a set. -use arrays inside class object -implement ADT with an interface and class templates -A set has the same operations as a bag except as noted below. The required changes for the set operations are: Add operation - Duplicate entries are not allowed in a set. If you add an entry that is already in the set, the set is not updated, but the operation is considered successful. If you add an entry that is not currently in the set, the entry needs to be added and the count of items in the set incremented by one. If the entry cannot be added for any reason, the operation fails. . Get frequency of operation - Delete this operation. Since duplicate entries are not allowed, the only possible results are zero or one. This information is already provided by the contains operation. . You will provide a default constructor that initializes the set to an empty set (like the bag constructor). Add a second constructor that has 2 parameters - an array of Item Type elements, and a count of the number of elements in the array. Use the array elements to initialize the set. Requirements 1. Vectors are used in the toVector() method of the class, and in the driver program that tests the class. Do not use vectors anywhere else. The main objective of this assignment is to use arrays inside class objects. 2. Do not use any C++ templates from the STL except as noted in Requirement 1. 3. You are encouraged to reuse class methods by calling them from other class methods (where appropriate). If you already have tested method in your class that will do what you need, use it instead of copying the code to your new method. Set Interface an interface for your ADT called SetInterface. Be sure to include the changes listed above that need to be included in the interface. Use the Baginterface class template as a model for your interface. You must include updated documentation for your set interface operations (methods) similar to the documentation in the Baginterface file. Set Implementation using arraysa class template called ArraySet to implement your ADT. You can combine the class template definition and class template implementation in a single header file (as I did in the example Bag code provided). Be sure to include the changes listed above that need to be included in the ArraySet class template. C++ (Object Oriented Programming) Recursion & Set ADT. The programming is divided in to two parts. First, we need a function that processes the elements of an array using recursion. Then, we implement a set class template using an array. Requirements: -Use of Binary search Find the largest element in an array using the recursive technique Each call to the recursive binary search function processes a portion of the array identified by the first and last indexes of the subarray of the subarray to be processes by this call. 1) First, we need a function called maxArray that will return the largest value in an integer array. A driver to test the function. The maxArray function and driver program can be in the same file Algorithm: Use the given algorithm to find the largest value in the array. if (anArray has only one entry) return the value of that entry else if (anArray has more than one entry) return the maximum of maxArray(left half of anArray) and maxArray(right half of anArray) BinarySearch function always returns value. For this program, your function will never be called with first > last. If familiar with the assert statement, you can use it to handle this error. Objectives A set is like a bag, except that duplicate entries are not allowed in a set. -use arrays inside class object -implement ADT with an interface and class templates -A set has the same operations as a bag except as noted below. The required changes for the set operations are: Add operation - Duplicate entries are not allowed in a set. If you add an entry that is already in the set, the set is not updated, but the operation is considered successful. If you add an entry that is not currently in the set, the entry needs to be added and the count of items in the set incremented by one. If the entry cannot be added for any reason, the operation fails. . Get frequency of operation - Delete this operation. Since duplicate entries are not allowed, the only possible results are zero or one. This information is already provided by the contains operation. . You will provide a default constructor that initializes the set to an empty set (like the bag constructor). Add a second constructor that has 2 parameters - an array of Item Type elements, and a count of the number of elements in the array. Use the array elements to initialize the set. Requirements 1. Vectors are used in the toVector() method of the class, and in the driver program that tests the class. Do not use vectors anywhere else. The main objective of this assignment is to use arrays inside class objects. 2. Do not use any C++ templates from the STL except as noted in Requirement 1. 3. You are encouraged to reuse class methods by calling them from other class methods (where appropriate). If you already have tested method in your class that will do what you need, use it instead of copying the code to your new method. Set Interface an interface for your ADT called SetInterface. Be sure to include the changes listed above that need to be included in the interface. Use the Baginterface class template as a model for your interface. You must include updated documentation for your set interface operations (methods) similar to the documentation in the Baginterface file. Set Implementation using arraysa class template called ArraySet to implement your ADT. You can combine the class template definition and class template implementation in a single header file (as I did in the example Bag code provided). Be sure to include the changes listed above that need to be included in the ArraySet class templateStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started