SlideShare a Scribd company logo
1 of 19
Download to read offline
/** 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;
vector toVector() const;
}; // end ArrayBag
//#include "ArrayBag.cpp" // /** Implementation file for the class ArrayBag. @file
ArrayBag.cpp */
#include "ArrayBag.h"
#include template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{ } // end default constructor
template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
template bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry; itemCount++;
} // end if return hasRoomToAdd;
} // end add
/* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB
} // end remove
*/ template bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if return
canRemoveItem;
} // end remove /*
// STUB template void ArrayBag::clear() { // STUB } // end clear
*/ template void ArrayBag::clear() { itemCount = 0; } // end clear
template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1: First version
template bool ArrayBag::contains(const ItemType& target) const
{
return getFrequencyOf(target) > 0;
} // end contains
// ALTERNATE 2: Second version
template bool ArrayBag::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
*/ template vector ArrayBag::toVector() const
{
vector bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found =
false; int result = -1;
int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target)
{
found = true;
result = searchIndex; }
else
{
searchIndex++;
} // end if
}
// end while return result;
} // end getIndexOf
#endif
/** Listing 1-1. @file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include using namespace std;
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 vector toVector() const = 0;
}; // end BagInterface
#endif
#include #include
#include "ArrayBag.h" using namespace std;
void displayBag(ArrayBag& bag)
{
cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl;
vector bagItems = bag.toVector();
int numberOfEntries = (int)bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void bagTester(ArrayBag& bag)
{
cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty() << ";should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
cout << "Try to add another entry: add("extra") returns " << bag.add("extra") << endl;
} // end bagTester
int main()
{
ArrayBag bag;
cout << "Testing the Array-Based Bag:" << endl;
cout << "The initial bag is empty." << endl;
bagTester(bag);
cout << "All done!" << endl;
system("pause");
return 0;
} // end main
#include "DateClass.h"
#include using namespace std;
int main()
{
Date date1;
Date date2(31, 12, 2015);
Date date3(29, 2, 2016);
Date date4(28, 2, 2016);
date1.printDate();
cout << " advanced by a day gives ";
date1.advanceDay();
date1.printDate();
cout << endl;
date2.printDate();
cout << " advanced by a day gives ";
date2.advanceDay();
date2.printDate();
cout << endl;
date3.printDate();
cout << " advanced by a day gives ";
date3.advanceDay();
date3.printDate();
cout << endl;
date4.printDate();
cout << " advanced by a day gives ";
date4.advanceDay();
date4.printDate();
cout << endl;
}
#include
#include "DateClass.h" using namespace std;
Date::Date()
{
day = 1;
month = 1;
year = 2016;
}
Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
bool IsLeap(int y)
{
return ((!(y % 4) && y % 100) || !(y % 400));
}
void Date::advanceDay()
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month
== 12)
{
//numOfDays = 31;
if (day == 31)
{
day = 1;
month++;
if (month == 13)
{
year++;
month = 1;
}
}
else day++;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
//numOfDays = 30;
if (day == 30)
{
day = 1;
month++;
}
else
day++;
}
if (month == 2)
{
if (IsLeap(year))
{
//numOfDays = 29;
if (day == 29)
{
day = 1;
month++;
}
else day++;
}
else
{
//numOfDays = 28;
if (day == 28)
{
day = 1;
month++;
}
else
day++;
}
}
}
void Date::printDate()
{
cout << day << "/" << month << "/" << year;
}
#include using namespace std;
class Date
{
int day;
int month;
int year;
public:
Date();
Date(int d, int m, int y);
int getDay();
int getMonth();
int getYear();
}
Void main()
{
Date.getDay(); //Get the day from the bag
Date.getMonth(); //Get the month from the bag
Date.getYear();//Get the year from the bag
cout<
Solution
/** 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;
vector toVector() const;
}; // end ArrayBag
//#include "ArrayBag.cpp" // /** Implementation file for the class ArrayBag. @file
ArrayBag.cpp */
#include "ArrayBag.h"
#include template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{ } // end default constructor
template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
template bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry; itemCount++;
} // end if return hasRoomToAdd;
} // end add
/* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB
} // end remove
*/ template bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if return
canRemoveItem;
} // end remove /*
// STUB template void ArrayBag::clear() { // STUB } // end clear
*/ template void ArrayBag::clear() { itemCount = 0; } // end clear
template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1: First version
template bool ArrayBag::contains(const ItemType& target) const
{
return getFrequencyOf(target) > 0;
} // end contains
// ALTERNATE 2: Second version
template bool ArrayBag::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains
*/ template vector ArrayBag::toVector() const
{
vector bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found =
false; int result = -1;
int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target)
{
found = true;
result = searchIndex; }
else
{
searchIndex++;
} // end if
}
// end while return result;
} // end getIndexOf
#endif
/** Listing 1-1. @file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include using namespace std;
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 vector toVector() const = 0;
}; // end BagInterface
#endif
#include #include
#include "ArrayBag.h" using namespace std;
void displayBag(ArrayBag& bag)
{
cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl;
vector bagItems = bag.toVector();
int numberOfEntries = (int)bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void bagTester(ArrayBag& bag)
{
cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
displayBag(bag);
string items[] = {"one", "two", "three", "four", "five", "one"};
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bag.add(items[i]);
} // end for displayBag(bag);
cout << "isEmpty: returns " << bag.isEmpty() << ";should be 0 (false)" << endl;
cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
cout << "Try to add another entry: add("extra") returns " << bag.add("extra") << endl;
} // end bagTester
int main()
{
ArrayBag bag;
cout << "Testing the Array-Based Bag:" << endl;
cout << "The initial bag is empty." << endl;
bagTester(bag);
cout << "All done!" << endl;
system("pause");
return 0;
} // end main
#include "DateClass.h"
#include using namespace std;
int main()
{
Date date1;
Date date2(31, 12, 2015);
Date date3(29, 2, 2016);
Date date4(28, 2, 2016);
date1.printDate();
cout << " advanced by a day gives ";
date1.advanceDay();
date1.printDate();
cout << endl;
date2.printDate();
cout << " advanced by a day gives ";
date2.advanceDay();
date2.printDate();
cout << endl;
date3.printDate();
cout << " advanced by a day gives ";
date3.advanceDay();
date3.printDate();
cout << endl;
date4.printDate();
cout << " advanced by a day gives ";
date4.advanceDay();
date4.printDate();
cout << endl;
}
#include
#include "DateClass.h" using namespace std;
Date::Date()
{
day = 1;
month = 1;
year = 2016;
}
Date::Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
bool IsLeap(int y)
{
return ((!(y % 4) && y % 100) || !(y % 400));
}
void Date::advanceDay()
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month
== 12)
{
//numOfDays = 31;
if (day == 31)
{
day = 1;
month++;
if (month == 13)
{
year++;
month = 1;
}
}
else day++;
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
//numOfDays = 30;
if (day == 30)
{
day = 1;
month++;
}
else
day++;
}
if (month == 2)
{
if (IsLeap(year))
{
//numOfDays = 29;
if (day == 29)
{
day = 1;
month++;
}
else day++;
}
else
{
//numOfDays = 28;
if (day == 28)
{
day = 1;
month++;
}
else
day++;
}
}
}
void Date::printDate()
{
cout << day << "/" << month << "/" << year;
}
#include using namespace std;
class Date
{
int day;
int month;
int year;
public:
Date();
Date(int d, int m, int y);
int getDay();
int getMonth();
int getYear();
}
Void main()
{
Date.getDay(); //Get the day from the bag
Date.getMonth(); //Get the month from the bag
Date.getYear();//Get the year from the bag
cout<

More Related Content

Similar to Header file for an array-based implementation of the ADT bag. @f.pdf

Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdfCharacter.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdftxkev
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxalanfhall8953
 
#include stdafx.h#include iostreamusing namespace std;cl.pdf
#include stdafx.h#include iostreamusing namespace std;cl.pdf#include stdafx.h#include iostreamusing namespace std;cl.pdf
#include stdafx.h#include iostreamusing namespace std;cl.pdfaashwini4
 
This is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfThis is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfinfo334223
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfdeepaksatrker
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfflashfashioncasualwe
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfanonaeon
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdfBANSALANKIT1077
 
public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdfannaimobiles
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfaksahnan
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxdickonsondorris
 
Symfony World - Symfony components and design patterns
Symfony World - Symfony components and design patternsSymfony World - Symfony components and design patterns
Symfony World - Symfony components and design patternsŁukasz Chruściel
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manualChandrapriya Jayabal
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdff3apparelsonline
 
Extending javascript part one
Extending javascript part oneExtending javascript part one
Extending javascript part oneVijaya Anand
 
Pleae help me with this C++ task to the required result by edit or f.pdf
Pleae help me with this C++ task to the required result by edit or f.pdfPleae help me with this C++ task to the required result by edit or f.pdf
Pleae help me with this C++ task to the required result by edit or f.pdfvinodagrawal6699
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfxlynettalampleyxc
 
Stack Implementation
Stack ImplementationStack Implementation
Stack ImplementationZidny Nafan
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxshericehewat
 

Similar to Header file for an array-based implementation of the ADT bag. @f.pdf (20)

Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdfCharacter.cpphpp givenCharacter.cpp#include Character.hp.pdf
Character.cpphpp givenCharacter.cpp#include Character.hp.pdf
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
 
#include stdafx.h#include iostreamusing namespace std;cl.pdf
#include stdafx.h#include iostreamusing namespace std;cl.pdf#include stdafx.h#include iostreamusing namespace std;cl.pdf
#include stdafx.h#include iostreamusing namespace std;cl.pdf
 
This is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfThis is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdf
 
This file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdfThis file contains a complete array-based MultiSet, but not the code.pdf
This file contains a complete array-based MultiSet, but not the code.pdf
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdf
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
 
public class DoubleArraySeq implements Cloneable {    Priva.pdf
public class DoubleArraySeq implements Cloneable {     Priva.pdfpublic class DoubleArraySeq implements Cloneable {     Priva.pdf
public class DoubleArraySeq implements Cloneable {    Priva.pdf
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
Symfony World - Symfony components and design patterns
Symfony World - Symfony components and design patternsSymfony World - Symfony components and design patterns
Symfony World - Symfony components and design patterns
 
(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual(674335607) cs2309 java-lab-manual
(674335607) cs2309 java-lab-manual
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
 
Extending javascript part one
Extending javascript part oneExtending javascript part one
Extending javascript part one
 
Pleae help me with this C++ task to the required result by edit or f.pdf
Pleae help me with this C++ task to the required result by edit or f.pdfPleae help me with this C++ task to the required result by edit or f.pdf
Pleae help me with this C++ task to the required result by edit or f.pdf
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
 

More from sudhirchourasia86

W since less electrons means difficult to melt so.pdf
                     W since less electrons means difficult to melt so.pdf                     W since less electrons means difficult to melt so.pdf
W since less electrons means difficult to melt so.pdfsudhirchourasia86
 
To the left. The ketone is more stable compared .pdf
                     To the left.  The ketone is more stable compared .pdf                     To the left.  The ketone is more stable compared .pdf
To the left. The ketone is more stable compared .pdfsudhirchourasia86
 
The true statement is D. Concentration affects .pdf
                     The true statement is  D. Concentration affects .pdf                     The true statement is  D. Concentration affects .pdf
The true statement is D. Concentration affects .pdfsudhirchourasia86
 
Smaller mass gases have greater rates of effusion.pdf
                     Smaller mass gases have greater rates of effusion.pdf                     Smaller mass gases have greater rates of effusion.pdf
Smaller mass gases have greater rates of effusion.pdfsudhirchourasia86
 
please send the question details I have gone thr.pdf
                     please send the question details  I have gone thr.pdf                     please send the question details  I have gone thr.pdf
please send the question details I have gone thr.pdfsudhirchourasia86
 
melting point of the compound increases due to th.pdf
                     melting point of the compound increases due to th.pdf                     melting point of the compound increases due to th.pdf
melting point of the compound increases due to th.pdfsudhirchourasia86
 
molality = moles of solute kg of solvent In thi.pdf
                     molality = moles of solute  kg of solvent In thi.pdf                     molality = moles of solute  kg of solvent In thi.pdf
molality = moles of solute kg of solvent In thi.pdfsudhirchourasia86
 
Ionic Equation is MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf
                     Ionic Equation is   MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf                     Ionic Equation is   MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf
Ionic Equation is MgO(s) + 2 H+(aq) + 2NO3- (aq.pdfsudhirchourasia86
 
FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf
                     FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf                     FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf
FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdfsudhirchourasia86
 
d) in real gases there are attraction between m.pdf
                     d)   in real gases there are attraction between m.pdf                     d)   in real gases there are attraction between m.pdf
d) in real gases there are attraction between m.pdfsudhirchourasia86
 
SolutionTraversing Binary Trees The preorder standard procedure.pdf
SolutionTraversing Binary Trees The preorder standard procedure.pdfSolutionTraversing Binary Trees The preorder standard procedure.pdf
SolutionTraversing Binary Trees The preorder standard procedure.pdfsudhirchourasia86
 
Shareholder’s equity= Current assets+Net fixed assets-Current lia.pdf
Shareholder’s equity= Current assets+Net fixed assets-Current lia.pdfShareholder’s equity= Current assets+Net fixed assets-Current lia.pdf
Shareholder’s equity= Current assets+Net fixed assets-Current lia.pdfsudhirchourasia86
 
in Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdf
in Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdfin Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdf
in Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdfsudhirchourasia86
 
   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf
   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf
   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdfsudhirchourasia86
 
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdfsudhirchourasia86
 
Initial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdf
Initial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdfInitial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdf
Initial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdfsudhirchourasia86
 
There are ten guidelines with a broad coverage, ranging from develop.pdf
There are ten guidelines with a broad coverage, ranging from develop.pdfThere are ten guidelines with a broad coverage, ranging from develop.pdf
There are ten guidelines with a broad coverage, ranging from develop.pdfsudhirchourasia86
 
The way Ive been told to look at the classifications is to look at.pdf
The way Ive been told to look at the classifications is to look at.pdfThe way Ive been told to look at the classifications is to look at.pdf
The way Ive been told to look at the classifications is to look at.pdfsudhirchourasia86
 

More from sudhirchourasia86 (20)

W since less electrons means difficult to melt so.pdf
                     W since less electrons means difficult to melt so.pdf                     W since less electrons means difficult to melt so.pdf
W since less electrons means difficult to melt so.pdf
 
To the left. The ketone is more stable compared .pdf
                     To the left.  The ketone is more stable compared .pdf                     To the left.  The ketone is more stable compared .pdf
To the left. The ketone is more stable compared .pdf
 
The true statement is D. Concentration affects .pdf
                     The true statement is  D. Concentration affects .pdf                     The true statement is  D. Concentration affects .pdf
The true statement is D. Concentration affects .pdf
 
Smaller mass gases have greater rates of effusion.pdf
                     Smaller mass gases have greater rates of effusion.pdf                     Smaller mass gases have greater rates of effusion.pdf
Smaller mass gases have greater rates of effusion.pdf
 
please send the question details I have gone thr.pdf
                     please send the question details  I have gone thr.pdf                     please send the question details  I have gone thr.pdf
please send the question details I have gone thr.pdf
 
O3, SO3, SO2 .pdf
                     O3, SO3, SO2                                     .pdf                     O3, SO3, SO2                                     .pdf
O3, SO3, SO2 .pdf
 
melting point of the compound increases due to th.pdf
                     melting point of the compound increases due to th.pdf                     melting point of the compound increases due to th.pdf
melting point of the compound increases due to th.pdf
 
molality = moles of solute kg of solvent In thi.pdf
                     molality = moles of solute  kg of solvent In thi.pdf                     molality = moles of solute  kg of solvent In thi.pdf
molality = moles of solute kg of solvent In thi.pdf
 
Ionic Equation is MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf
                     Ionic Equation is   MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf                     Ionic Equation is   MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf
Ionic Equation is MgO(s) + 2 H+(aq) + 2NO3- (aq.pdf
 
FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf
                     FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf                     FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf
FeBrs is a typo Lets suppose it is FeBr2. 2Na .pdf
 
Density increases .pdf
                     Density increases                                .pdf                     Density increases                                .pdf
Density increases .pdf
 
d) in real gases there are attraction between m.pdf
                     d)   in real gases there are attraction between m.pdf                     d)   in real gases there are attraction between m.pdf
d) in real gases there are attraction between m.pdf
 
SolutionTraversing Binary Trees The preorder standard procedure.pdf
SolutionTraversing Binary Trees The preorder standard procedure.pdfSolutionTraversing Binary Trees The preorder standard procedure.pdf
SolutionTraversing Binary Trees The preorder standard procedure.pdf
 
Shareholder’s equity= Current assets+Net fixed assets-Current lia.pdf
Shareholder’s equity= Current assets+Net fixed assets-Current lia.pdfShareholder’s equity= Current assets+Net fixed assets-Current lia.pdf
Shareholder’s equity= Current assets+Net fixed assets-Current lia.pdf
 
in Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdf
in Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdfin Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdf
in Reproductive cloning of mammals the Nucelus ( Genetic material) f.pdf
 
   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf
   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf
   a) OH- (aq) is a Lewis base because it can give electrions to Oth.pdf
 
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
 
Initial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdf
Initial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdfInitial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdf
Initial concentration of NH3 = molesvolume = 0.2501.00 = 0.250 M.pdf
 
There are ten guidelines with a broad coverage, ranging from develop.pdf
There are ten guidelines with a broad coverage, ranging from develop.pdfThere are ten guidelines with a broad coverage, ranging from develop.pdf
There are ten guidelines with a broad coverage, ranging from develop.pdf
 
The way Ive been told to look at the classifications is to look at.pdf
The way Ive been told to look at the classifications is to look at.pdfThe way Ive been told to look at the classifications is to look at.pdf
The way Ive been told to look at the classifications is to look at.pdf
 

Recently uploaded

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

Header file for an array-based implementation of the ADT bag. @f.pdf

  • 1. /** 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; vector toVector() const; }; // end ArrayBag //#include "ArrayBag.cpp" // /** Implementation file for the class ArrayBag. @file ArrayBag.cpp */ #include "ArrayBag.h" #include template ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) { } // end default constructor template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty template bool ArrayBag::add(const ItemType& newEntry) {
  • 2. bool hasRoomToAdd = (itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry; itemCount++; } // end if return hasRoomToAdd; } // end add /* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB } // end remove */ template bool ArrayBag::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; items[locatedIndex] = items[itemCount]; } // end if return canRemoveItem; } // end remove /* // STUB template void ArrayBag::clear() { // STUB } // end clear */ template void ArrayBag::clear() { itemCount = 0; } // end clear template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex < itemCount) { if (items[curIndex] == anEntry) { frequency++; } // end if curIndex++; // Increment to next entry } // end while return frequency; } // end getFrequencyOf template bool ArrayBag::contains(const ItemType& anEntry) const
  • 3. { return getIndexOf(anEntry) > -1; } // end contains /* ALTERNATE 1: First version template bool ArrayBag::contains(const ItemType& target) const { return getFrequencyOf(target) > 0; } // end contains // ALTERNATE 2: Second version template bool ArrayBag::contains(const ItemType& anEntry) const { bool found = false; int curIndex = 0; // Current array index while (!found && (curIndex < itemCount)) { if (anEntry == items[curIndex]) { found = true; } // end if curIndex++; // Increment to next entry } // end while return found; } // end contains */ template vector ArrayBag::toVector() const { vector bagContents; for (int i = 0; i < itemCount; i++) bagContents.push_back(items[i]); return bagContents; } // end toVector // private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target) { found = true;
  • 4. result = searchIndex; } else { searchIndex++; } // end if } // end while return result; } // end getIndexOf #endif /** Listing 1-1. @file BagInterface.h */ #ifndef _BAG_INTERFACE #define _BAG_INTERFACE #include using namespace std; 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;
  • 5. /** 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 vector toVector() const = 0; }; // end BagInterface #endif #include #include #include "ArrayBag.h" using namespace std; void displayBag(ArrayBag& bag) { cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl; vector bagItems = bag.toVector(); int numberOfEntries = (int)bagItems.size(); for (int i = 0; i < numberOfEntries; i++) { cout << bagItems[i] << " "; } // end for cout << endl << endl; } // end displayBag void bagTester(ArrayBag& bag) { cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl; displayBag(bag); string items[] = {"one", "two", "three", "four", "five", "one"}; cout << "Add 6 items to the bag: " << endl; for (int i = 0; i < 6; i++) { bag.add(items[i]); } // end for displayBag(bag); cout << "isEmpty: returns " << bag.isEmpty() << ";should be 0 (false)" << endl; cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl; cout << "Try to add another entry: add("extra") returns " << bag.add("extra") << endl; } // end bagTester int main()
  • 6. { ArrayBag bag; cout << "Testing the Array-Based Bag:" << endl; cout << "The initial bag is empty." << endl; bagTester(bag); cout << "All done!" << endl; system("pause"); return 0; } // end main #include "DateClass.h" #include using namespace std; int main() { Date date1; Date date2(31, 12, 2015); Date date3(29, 2, 2016); Date date4(28, 2, 2016); date1.printDate(); cout << " advanced by a day gives "; date1.advanceDay(); date1.printDate(); cout << endl; date2.printDate(); cout << " advanced by a day gives "; date2.advanceDay(); date2.printDate(); cout << endl; date3.printDate(); cout << " advanced by a day gives "; date3.advanceDay(); date3.printDate(); cout << endl; date4.printDate(); cout << " advanced by a day gives "; date4.advanceDay(); date4.printDate();
  • 7. cout << endl; } #include #include "DateClass.h" using namespace std; Date::Date() { day = 1; month = 1; year = 2016; } Date::Date(int d, int m, int y) { day = d; month = m; year = y; } int Date::getDay() { return day; } int Date::getMonth() { return month; } int Date::getYear() { return year; } bool IsLeap(int y) { return ((!(y % 4) && y % 100) || !(y % 400)); } void Date::advanceDay() { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  • 8. { //numOfDays = 31; if (day == 31) { day = 1; month++; if (month == 13) { year++; month = 1; } } else day++; } if (month == 4 || month == 6 || month == 9 || month == 11) { //numOfDays = 30; if (day == 30) { day = 1; month++; } else day++; } if (month == 2) { if (IsLeap(year)) { //numOfDays = 29; if (day == 29) { day = 1; month++; } else day++;
  • 9. } else { //numOfDays = 28; if (day == 28) { day = 1; month++; } else day++; } } } void Date::printDate() { cout << day << "/" << month << "/" << year; } #include using namespace std; class Date { int day; int month; int year; public: Date(); Date(int d, int m, int y); int getDay(); int getMonth(); int getYear(); } Void main() { Date.getDay(); //Get the day from the bag Date.getMonth(); //Get the month from the bag Date.getYear();//Get the year from the bag
  • 10. cout< Solution /** 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; vector toVector() const; }; // end ArrayBag //#include "ArrayBag.cpp" // /** Implementation file for the class ArrayBag. @file ArrayBag.cpp */ #include "ArrayBag.h" #include template ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) { } // end default constructor template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
  • 11. template bool ArrayBag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry; itemCount++; } // end if return hasRoomToAdd; } // end add /* // STUB template bool ArrayBag::remove(const ItemType& anEntry) { return false; // STUB } // end remove */ template bool ArrayBag::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; items[locatedIndex] = items[itemCount]; } // end if return canRemoveItem; } // end remove /* // STUB template void ArrayBag::clear() { // STUB } // end clear */ template void ArrayBag::clear() { itemCount = 0; } // end clear template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex < itemCount) { if (items[curIndex] == anEntry) { frequency++; } // end if curIndex++; // Increment to next entry } // end while return frequency;
  • 12. } // end getFrequencyOf template bool ArrayBag::contains(const ItemType& anEntry) const { return getIndexOf(anEntry) > -1; } // end contains /* ALTERNATE 1: First version template bool ArrayBag::contains(const ItemType& target) const { return getFrequencyOf(target) > 0; } // end contains // ALTERNATE 2: Second version template bool ArrayBag::contains(const ItemType& anEntry) const { bool found = false; int curIndex = 0; // Current array index while (!found && (curIndex < itemCount)) { if (anEntry == items[curIndex]) { found = true; } // end if curIndex++; // Increment to next entry } // end while return found; } // end contains */ template vector ArrayBag::toVector() const { vector bagContents; for (int i = 0; i < itemCount; i++) bagContents.push_back(items[i]); return bagContents; } // end toVector // private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target)
  • 13. { found = true; result = searchIndex; } else { searchIndex++; } // end if } // end while return result; } // end getIndexOf #endif /** Listing 1-1. @file BagInterface.h */ #ifndef _BAG_INTERFACE #define _BAG_INTERFACE #include using namespace std; 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
  • 14. 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 vector toVector() const = 0; }; // end BagInterface #endif #include #include #include "ArrayBag.h" using namespace std; void displayBag(ArrayBag& bag) { cout << "The bag contains " << bag.getCurrentSize() << " items:" << endl; vector bagItems = bag.toVector(); int numberOfEntries = (int)bagItems.size(); for (int i = 0; i < numberOfEntries; i++) { cout << bagItems[i] << " "; } // end for cout << endl << endl; } // end displayBag void bagTester(ArrayBag& bag) { cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl; displayBag(bag); string items[] = {"one", "two", "three", "four", "five", "one"}; cout << "Add 6 items to the bag: " << endl; for (int i = 0; i < 6; i++) { bag.add(items[i]); } // end for displayBag(bag); cout << "isEmpty: returns " << bag.isEmpty() << ";should be 0 (false)" << endl; cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl; cout << "Try to add another entry: add("extra") returns " << bag.add("extra") << endl;
  • 15. } // end bagTester int main() { ArrayBag bag; cout << "Testing the Array-Based Bag:" << endl; cout << "The initial bag is empty." << endl; bagTester(bag); cout << "All done!" << endl; system("pause"); return 0; } // end main #include "DateClass.h" #include using namespace std; int main() { Date date1; Date date2(31, 12, 2015); Date date3(29, 2, 2016); Date date4(28, 2, 2016); date1.printDate(); cout << " advanced by a day gives "; date1.advanceDay(); date1.printDate(); cout << endl; date2.printDate(); cout << " advanced by a day gives "; date2.advanceDay(); date2.printDate(); cout << endl; date3.printDate(); cout << " advanced by a day gives "; date3.advanceDay(); date3.printDate(); cout << endl; date4.printDate(); cout << " advanced by a day gives ";
  • 16. date4.advanceDay(); date4.printDate(); cout << endl; } #include #include "DateClass.h" using namespace std; Date::Date() { day = 1; month = 1; year = 2016; } Date::Date(int d, int m, int y) { day = d; month = m; year = y; } int Date::getDay() { return day; } int Date::getMonth() { return month; } int Date::getYear() { return year; } bool IsLeap(int y) { return ((!(y % 4) && y % 100) || !(y % 400)); } void Date::advanceDay() {
  • 17. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { //numOfDays = 31; if (day == 31) { day = 1; month++; if (month == 13) { year++; month = 1; } } else day++; } if (month == 4 || month == 6 || month == 9 || month == 11) { //numOfDays = 30; if (day == 30) { day = 1; month++; } else day++; } if (month == 2) { if (IsLeap(year)) { //numOfDays = 29; if (day == 29) { day = 1; month++;
  • 18. } else day++; } else { //numOfDays = 28; if (day == 28) { day = 1; month++; } else day++; } } } void Date::printDate() { cout << day << "/" << month << "/" << year; } #include using namespace std; class Date { int day; int month; int year; public: Date(); Date(int d, int m, int y); int getDay(); int getMonth(); int getYear(); } Void main() { Date.getDay(); //Get the day from the bag
  • 19. Date.getMonth(); //Get the month from the bag Date.getYear();//Get the year from the bag cout<