//CS2310 Exercise 14 // //Name ___________________ SSN ______________________ // //Chapter 15: p894 - 3 // // IMPLEMENTATION FILE (expArray.cpp) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, // allows aggregate array copying and initialization. // allows expanding the size of array when needed. // #include #include "expArray.h" using namespace std; ExpArray::ExpArray( /* in */ int arrSize ) // Constructor. // PRE: arrSize is assigned // POST: IF arrSize >= 1 && enough memory THEN // Array of size arrSize is created with // all elements == 0 ELSE error message. { int i; if ( arrSize < 1 ) { cerr << "ExpArray constructor - invalid size: " << arrSize << endl; exit(1); } arr = new int[arrSize] ; // allocate memory size = arrSize; for (i = 0; i < size; i++) arr[i] = 0; } void ExpArray::Store ( /* in */ int val, /* in */ int i ) // PRE: val and i are assigned // POST: IF 0 <= i < size of this array THEN // arr[i] == val // ELSE error message. { if ( i < 0 || i >= size ) { cerr << "Store - invalid index : " << i << endl; exit(1) ; } arr[i] = val ; } int ExpArray::ValueAt ( /* in */ int i ) const // PRE: i is assigned. // POST: IF 0 <= i < size THEN // FCTVAL == arr[i] // ELSE halt with error message. { if ( i < 0 || i >= size ) { cerr << "ValueAt - invalid index : " << i << endl; exit(1) ; } return arr[i]; } ExpArray::~ExpArray( ) // Destructor. // POST: Memory for dynamic array deallocated. { delete [ ] arr ; } void ExpArray::ExpandBy( /* in */ int n) // Precondition: // n > 0 // Postcondition: // Size of array has increased by n elements // && All of the additional n elements equal zero { int* tempArr; int newSize, i; newSize = size + n; tempArr = new // Write the missing code here: // create a new array of new size. for (i=0; i