Question
Need help C++. Stuck on problem from hwk. Need help, it's due today! The method/function is called increaseSize() this is where i'm having problems the
Need help C++. Stuck on problem from hwk. Need help, it's due today! The method/function is called increaseSize() this is where i'm having problems the code just freezes, no crash, nothing happens. I'm trying to double the array size in my class. Please help it's due today!
HWK: If the array has reached its capacity, the add function should call a helper function to double the size of the array. The helper function will dynamically allocate storage for an array of twice the size, copy all the elements in the original array and update all the data members. The original pointer to the array should point to this new storage area after the copy is complete. The add function will then add the new element to the array.
here is my code:
main.cpp:
#include#include #include #include "container class.h" #include "container class.cpp" const int WIDTH = 70; const char border = '#'; using namespace std; int main() { Container<int> objectInt; Container<double> objectDbl; string name = "Alex"; int contents[10], option = 0, elements, input = -1, newElement, index; double contents2[10]; //top Border menu 1 cout << ' '; cout << setfill(border) << setw(WIDTH) << right << border << ' '; cout << border << setfill(' ') << setw(WIDTH - 1) << border << ' '; //title and options for user cout << border << ' ' << left << setw(WIDTH - 3) << " Welcome, " + name + '!' << border << ' '; cout << border << setfill(' ') << setw(WIDTH - 1) << right << border << ' '; cout << border << ' ' << left << setw(WIDTH - 3) << "Enter number of elements for array between 1-10:" << border << endl; cout << border << setfill(' ') << setw(WIDTH - 1) << right << border << ' '; cout << setfill(border) << setw(WIDTH) << right << border << " "; // end menu 1 cin >> elements; while (elements > 10 || elements < 1){ cout << "Try again. You must enter a value between 1-10. "; cin >> elements; } //top Border menu 2 cout << ' '; cout << setfill(border) << setw(WIDTH) << right << border << ' '; cout << border << setfill(' ') << setw(WIDTH - 1) << border << ' '; //title and options for user cout << border << ' ' << left << setw(WIDTH - 3) << " Welcome, " + name + '!' << border << ' '; cout << border << setfill(' ') << setw(WIDTH - 1) << right << border << ' '; cout << border << ' ' << left << setw(WIDTH - 3) << "Enter values to store in array: " << border << endl; cout << border << setfill(' ') << setw(WIDTH - 1) << right << border << ' '; cout << setfill(border) << setw(WIDTH) << right << border << " "; // end menu 2 for (index = 0; index < elements/*10*/; index++) { cin >> contents[index]; objectInt.add(contents[index]); } while(option != 9){ // Main Menu objectInt.menu(); // end menu cin >> option; newElement = 10; while (option < 1 || option > 9) { cout << "Try again. You must enter a value between 1-9. "; cin >> option; } switch (option) { case 1 : if (!objectInt.isFull()) { cout << "Enter a value to add: "; cin >> contents[index]; cout << contents[index] << endl; objectInt.add(contents[index]); cout << "New data: "; objectInt.displayContents(); } else { objectInt.increaseSize(); cout << "Enter a value to add: "; cin >> contents[index]; cout << contents[index] << endl; objectInt.add(contents[index]); cout << "New data: "; objectInt.displayContents(); /*cout << "Sorry the array is full. ";*/ } break; case 2 : if(!objectInt.isEmpty()){ cout << "Enter value to remove: "; cin >> input; objectInt.remove(input); cout << "New data: "; objectInt.displayContents(); } else{ cout << "No element to remove. "; } break; case 3 : cout << "Enter new value: "; cin >> input; cout << "Enter element to replace with new value: "; cin >> newElement; objectInt.setNew(newElement, input); cout << "New data: "; objectInt.displayContents(); break; case 4: cout << "Enter value to return an element: "; cin >> input; if(objectInt.getIndexOf(input) == -1) { cout << "Invalid input. Returning to main menu. Try again. ";} else{ cout << "Element is: " << objectInt.getIndexOf(input) << endl; } break; case 5: cout << "Enter element position: "; cin >> input; cout << "Element: " << input << " contains value: " << objectInt.getElement(input); break; case 6: while(newElement > objectInt.getCurrentElements() || newElement > 10) { cout << "enter element between 0-" << objectInt.getCurrentElements() << ": "; cin >> newElement; if(newElement > objectInt.getCurrentElements() || newElement > 10) cout << "Element must be between 0-" << objectInt.getCurrentElements() << ". Try again. "; } objectInt.removeElement(newElement); break; case 7: while(newElement > objectInt.getCurrentElements() || newElement > 10) { cout << "enter element between 0-" << objectInt.getCurrentElements() << ": "; cin >> newElement; cout << "enter new value: "; cin >> input; if(newElement > objectInt.getCurrentElements() || newElement > 10) cout << "Element must be between 0-" << objectInt.getCurrentElements() << ". Try again. "; } objectInt.addNewElement(newElement, input); break; case 8: objectInt.displayContents(); break; case 9: return 0; default : cout << "Oops something went wrong. "; } } return 0; }
container class.cpp:
#include#include #include #include "container class.h" //default constructor template<class ItemType> Container ::Container(): itemCount(0), maxItems(DEFAULT_SIZE) { items = new ItemType[DEFAULT_SIZE]; }// end default constructor // destructor template<class ItemType> Container ::~Container(){ delete[] items; }// end destructor // increaseSize template<class ItemType> int Container ::increaseSize(){ int newSize = DEFAULT_SIZE; //items = new ItemType[DEFAULT_SIZE]; /*int* increase = new int[newSize], */ int i = 0; while (std::cin >> items[i]){ i++; if ( i >= newSize){ newSize = newSize * 2; // double previous size int *deleteMe = new int[newSize]; // create new bigger array for (int j = 0; j < i; j++) deleteMe[j] = items[j]; // copy values to new array delete[] items; // free memory of old array items = deleteMe; // point to new array } } } // getCurrentSize template<class ItemType> int Container ::getCurrentSize() const { return itemCount; } // end getCurrentSize template<class ItemType> int Container ::getCurrentElements() const { return itemCount - 1; } // end getCurrentSized template<class ItemType> bool Container ::isEmpty() const { return itemCount == 0; } // end isEmpty template<class ItemType> bool Container ::isFull() const { bool full; if (itemCount == maxItems) full = true; else full = false; return full; } // end isEmpty template<class ItemType> bool Container ::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry; itemCount++; } // end if return hasRoomToAdd; } // end add template<class ItemType> bool Container ::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 // setNew template<class ItemType> bool Container ::setNew(const ItemType& anEntry, const ItemType& num){ bool canRemoveItem = !isEmpty() && (anEntry > -1); if (canRemoveItem) { items[anEntry] = num; } // end if return canRemoveItem; }// end setNew template<class ItemType> bool Container ::addNewElement(const ItemType& anElement, const ItemType& newVal){ bool canAddItem = !isEmpty() && !isFull() && (anElement > -1); if (canAddItem) { itemCount++; for (int i = anElement; i <= itemCount; i++){ items[i+1] = items[i] ; } items[anElement] = newVal; } // end if return canAddItem; }// end addNewElement template<class ItemType> bool Container ::removeElement(const ItemType& anElement){ bool canAddItem = !isEmpty() && !isFull() && (anElement > -1); if (canAddItem) { for (int i = anElement; i <= itemCount; i++){ items[i] = items[i+1]; } itemCount--; } // end if return canAddItem; }//end removeElement template<class ItemType> void Container ::clear() { itemCount = 0; } // end clear template<class ItemType> int Container ::getIndexOf(const ItemType &target) const { bool isFound = false; int result = -1; int searchIndex = 0; // If the container is empty, itemCount is zero, so loop is skipped while (!isFound && (searchIndex < itemCount)) { isFound = (items[searchIndex] == target); if (isFound) { result = searchIndex; } else { searchIndex++; } // end if } // end while return result; } // end getIndexOf // getElement template<class ItemType> int Container ::getElement(const ItemType &target) const{ int getElement; for (int i = 0; i <= itemCount; i++){ if (target == i) /*std::cout << "items[i]: " << items[i] << std::endl;*/ getElement = items[i]; } return getElement; }// end getElement template<class ItemType> bool Container ::contains(const ItemType& anEntry) const { bool isFound = false; int curIndex = 0; // Current array index while (!isFound && (curIndex < itemCount)) { isFound = (anEntry == items[curIndex]); if (!isFound) curIndex++; // Increment to next entry } // end while return isFound; } // end contains template<class ItemType> int Container ::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<class ItemType> void Container ::displayContents(){ int index = 0; std::cout << "Array contains |element |value|" << std::endl; for (index = 0; index < itemCount; index++){ std::cout << " " << index << " "; std::cout << " " < template<class ItemType> void Container ::menu(){ std::string name = "Alex"; const int WIDTH = 70; const char border = '#'; std::cout << ' '; std::cout << std::setfill(border) << std::setw(WIDTH) << std::right << border << ' '; std::cout << border << std::setfill(' ') << std::setw(WIDTH - 1) << border << ' '; //title and options for user std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << " Welcome, " + name + '!' << border << ' '; std::cout << border << std::setfill(' ') << std::setw(WIDTH - 1) << std::right << border << ' '; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "Please select one of the following integers: " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "1. Add value to array " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "2. Remove value from array " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "3. Set value of element " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "4. Return element at given postion " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "5. Return the position of an element passed as an argument " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "6. Delete an element at a given position " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "7. Insert an element at a given position " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "8. Display contents " << border << std::endl; std::cout << border << ' ' << std::left << std::setw(WIDTH - 3) << "9. Exit " << border << std::endl; std::cout << border << std::setfill(' ') << std::setw(WIDTH - 1) << std::right << border << ' '; std::cout << std::setfill(border) << std::setw(WIDTH) << std::right << border << " "; } template<class ItemType> std::vector Container ::toVector() const { std::vector containerContents; for (int i = 0; i < itemCount; i++) containerContents.push_back(items[i]); return containerContents; } // end toVector
container class.h:
#ifndef DYNAMIC_ARRAYS_AND_POINTERS_CONTAINER_CLASS_H #define DYNAMIC_ARRAYS_AND_POINTERS_CONTAINER_CLASS_H #includetemplate<class ItemType> class Container { private: int DEFAULT_SIZE = 10; ItemType* items; // array of container items int itemCount; // current count of container items int maxItems; // max capacity of the container // 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. public: Container();//constructor ~Container();// destructor int increaseSize();//expand array size void menu(); int getCurrentSize() const; int getCurrentElements() const; int getIndexOf(const ItemType &target) const; int getElement(const ItemType &target) const; bool isEmpty() const; bool isFull() const; bool add(const ItemType& newEntry); bool remove(const ItemType& anEntry); bool setNew(const ItemType& anEntry, const ItemType& num); bool addNewElement(const ItemType& anElement, const ItemType& newVal); bool removeElement(const ItemType& anElement); void clear(); bool contains(const ItemType& anEntry) const; int getFrequencyOf(const ItemType& anEntry) const; void displayContents(); std::vector toVector() const; }; // end container #endif
Step 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