Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef BOUNDEDARRAYCONST_H #define BOUNDEDARRAYCONST_H class BoundedArrayConst { public: // Create bounded array with no elements BoundedArrayConst(); // Create bounded array with specified number of elements

image text in transcribed

#ifndef BOUNDEDARRAYCONST_H #define BOUNDEDARRAYCONST_H class BoundedArrayConst { public: // Create bounded array with no elements BoundedArrayConst(); // Create bounded array with specified number of elements (all zeros). Throw // out of range exception for invalid size. BoundedArrayConst(int numElements); // Create bounded array with array elements (not just pointer) copied from b BoundedArrayConst(BoundedArrayConst& b); ~BoundedArrayConst(); // Return the number of elements in the bounded array int size(); // Return the maximum number of elements in the bounded array int capacity(); // Return element at specified index with no error checking double& operator[](int index); // Return element at specified index. Throw out of range exception for invalid // index. double& at(int index); // Return pointer to array double* data(); // Copy array elements from b into current array BoundedArrayConst& operator=(BoundedArrayConst& b); // Change number of elements in the bounded array void resize(int newSize); // Insert a new element with specified value at the end of the bounded array. // Throw out of range exception if the bounded array is full. void push_back(double value); // Delete last element in the bounded array. Throw out of range exception if // array is empty. void pop_back(); // Insert a new element with specified value at the specified index, moving all // other elements back by 1. Throw out of range exception if index is invalid or // the bounded array is full. void insert(int index, double value); // Delete element at specified index. Throw out of range exception if index is // invalid. void erase(int index); // Delete all elements and reset the bounded array to size 0. void clear(); private: // Maximum number of elements (capacity) the array can store static const int MAX_NUM_ELEMENTS = 100; // Actual number of elements in the array int numElements = 0; // Array containing elements double elements[MAX_NUM_ELEMENTS]; }; #endif // !BOUNDEDARRAYCONST_H

#include  #include  #include "BoundedArrayConst.h" BoundedArrayConst::BoundedArrayConst() { for (int i = 0; i = numElements) throw std::out_of_range("Invalid index " + std::to_string(index)); return operator[](index); } double* BoundedArrayConst::data() { return elements; } BoundedArrayConst& BoundedArrayConst::operator=(BoundedArrayConst& b) { numElements = b.size(); for (int i = 0; i  MAX_NUM_ELEMENTS || newSize = MAX_NUM_ELEMENTS) throw std::out_of_range("Array has reached its maximum capacity"); elements[numElements] = value; numElements++; } void BoundedArrayConst::pop_back() { if (numElements == 0) throw std::out_of_range("Array has no elements"); numElements--; elements[numElements] = 0; } void BoundedArrayConst::insert(int index, double value) { if (index >= numElements || index = MAX_NUM_ELEMENTS) throw std::out_of_range("Array has reached its maximum capacity"); for (int i = numElements; i > index; i--) elements[i] = elements[i - 1]; elements[index] = value; numElements++; } void BoundedArrayConst::erase(int index) { if (index >= numElements || index   In some application settings, it is useful to have a fixed-capacity array that can store up to M elements. The actual size m of the array also sometimes called the logical size) can be less than M, but it can be grown up to the capacity M, as shown in Figure 1 below. This type of data structure is sometimes referred to as a bounded array since the array size can grow but has a hard upper bound. 271/384 Logical size Capacity Figure 1. Illustration of a bounded array. The capacity denotes the number of contiguous storage locations allocated in memory. The actual or logical size of the array denotes the number of elements stored in the array and can increase up to the capacity. The header and source files BoundedArrayConst.h and BoundedArrayConst.cpp contain an implemen- tation of a bounded array data structure to store variables of type double where the capacity is specified by a constant. The member functions have been created to closely match those of the STL vector class. Your task is to modify the implementation of BoundedArrayConst so that the capacity is specified by a regular variable rather than a constant. Call the new class BoundedArray and make the following changes to BoundedArrayConst:  Set the default value for the capacity to be 100 elements.  Add a constructor with the following header to allow the user of the class to choose the capacity along with the number of elements: BoundedArray(int numElements, int maxNumElements);  Modify all included member functions as necessary to ensure their correct operation. To do this, you will have to use dynamic memory allocation using pointers and the new and delete operators. Note: You may not use any external container classes in this problem, such as the vector or array classes in the STL! You can and certainly are expected to use arrays in your implementation, but not the array class in the CHSTL! Submit your header file and source code in files named BoundedArray.h and BoundedArray.cpp, respectively

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions

Question

2. What, according to Sergey, was strange at this meeting?

Answered: 1 week ago