Question: 1. Add following member function (method) to the class template Bag: bool removeMin(); 2. Using the class template Bag we discussed in the Instruction 8
1. Add following member function (method) to the class template Bag: bool removeMin(); 2. Using the class template Bag we discussed in the Instruction 8 with no additional member functions added except those member functions we covered in Instruction 8 and the member functions you were asked to add in Assignment 6 to write a program in a .cpp file that meet following requirements: a. Using the class template to declare an object with ItemType being int b. Using random function to randomly generate 10 integers between 0 and 99 and add them to the object declared in step a. c. Remove the largest and the smallest numbers from the bag d. Calculate and display the average of the remaining numbers in the bag. thats my header file and i m trying to add get average. Calculate and display the average of the remaining numbers in the bag. #ifndef _Bag #define _Bag #include using namespace std; template class Bag { private: ItemType items[100]; int itemCount; public: Bag(); // default constructor int getItemCount(); bool isEmpty(); void add(ItemType item); void remove(ItemType item); void display(); void clear(); bool contains(ItemType anItem); bool removemax(); bool removeMin(); ItemType getSum(); }; //Definition of Constructors template Bag::Bag() { itemCount = 0; } // (Member Functions) template int Bag::getItemCount() { return itemCount; } template bool Bag::isEmpty() { return(itemCount == 0); } template void Bag::add(ItemType item) { if (itemCount == 100) cout << "The bag is full!" << endl; else { items[itemCount] = item; itemCount++; } } template void Bag::display() { cout << "The bag contains following integers: " << endl; for (int i = 0; i < itemCount; i++) cout << items[i] << endl; } template void Bag::clear() { itemCount = 0; } template bool Bag::contains(ItemType anItem) { for (int i = 0; i < itemCount; i++) { if (items[i] == anItem) return true; } return false; } template void Bag::remove(ItemType item) { if (isEmpty()) { cout << "Removal is failed! The bag is empty!" << endl; } else if (!contains(item)) { cout << "Removal is failed! The item is not in the bag." << endl; } else { // find the index of the item int index = 0; for (int i = 0; i < itemCount; i++) { if (items[i] == item) { index = i; break; } } for (int i = index; i < itemCount - 1; i++) { items[i] = items[i + 1]; } itemCount--; } } template bool Bag::removeMin() { if (isEmpty()) return false; else { ItemType a = items[0]; for(int i = 1; i < itemCount; i++){ if (items[i] < a) a = items[i]; } remove(a); return true; } } template bool Bag::removemax() { if (isEmpty()) return false; else { ItemType a = items[0]; for (int i = 1; i < itemCount; i++) { if (items[i] > a) a = items[i]; } remove(a); return true; } } template ItemType Bag::getSum() { if (isEmpty()) exit(1); else { ItemType a = items[0]; for (int i = 1; i < itemCount; i++) { a += items[i]; } return a; } } #endif _Bag source file #include #include"newBag.h" using namespace std; int main() { Bag intBag; Bag charBag; for (int i = 0; i < 12; i++) { intBag.add(rand() % 100); } intBag.display(); intBag.removeMin(); intBag.removemax(); cout << "=============="; intBag.display(); cout << "the sum is:" << intBag.getSum() << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
