Question
Can someone help me with this C++ program using partial arrays? All details are below. The task in this assignment is to update the provided
Can someone help me with this C++ program using partial arrays? All details are below.
The task in this assignment is to update the provided Array class to manage the
situation where the internal array can be partially filled. Up to this point, the examples we have
worked with have assumed that the entire array was filled with useful data. This assignment will
address a situation where the array has been allocated with a specific capacity, but only a subset
of the elements are being used.
To facilitate this, instead of having just one class variable to store the size of the array (previously
called size, or items), there will be two variables. The first, called capacity, will store the actual
allocated size of the array. The second, called numUsed, will store the number of elements in the
array that are actually being used. It is assumed that the used elements are stored in locations
zero through numUsed1. Obviously, numUsed must be <= capacity.
You are provided with an Array.h file in which the Array class definition has been
updated to reflect this change, with both capacity and numUsed defined. In addition, the set
and get methods for capacity and numUsed have been defined and implemented.
Beyond that, all defined methods for the Array class are implemented but without
any reference to numUsed. In other words, the class behaves as if numUsed did not exist (i.e. the
class behaves as if the array was still fully used).
The first part of the assignment is to go through each method and update it, as needed, to
implement partial array usage. The second part is to ensure that your changes work correctly by
testing them with the provided itest.cpp program. This will include adding additional tests at the
end of itest.cpp as indicated in the comments. Finally, you are required to create two new test
programs, ftest.cpp and stest.cpp, that will test the template class for use with float and string
data types. (You can copy and edit itest.cpp, as needed, to accomplish this)
Implementation details:
The first problem to address is the constructors. There are three of them. It will be necessary to
set not only the capacity, as the internal arrays are allocated, but the number of elements in use,
as follows:
Default constructor: number of elements in use should be zero
Constructor from eltType array: number of elements in use will be given as an argument. Note it
must be <= to allocated array size (capacity)!
Copy constructor: make sure everything is copied, including number of elements in use!
Operators:
Assignment operator (=): Like the copy constructor (watch out for selfassignment)
Comparison operators (== and !=): Only compare the elements in use! The unused values and
overall array capacities dont matter.
Plus equals(+=): A special case. If the number of elements in use is less than the capacity, just add
the new element in the next available unused array location. If the array is full (number of
elements equals capacity), then you will need to expand the size of the array, as was done in
existing examples.
Subscript operators ([]): A special case again two possibilities.
1. For the const version, an error should occur if a user attempts to access an unused
location.
2. For the nonconst version, any location index within the array capacity is valid but!... if
the requested location is a previously unused location (i.e.> numUsed), then numUsed
must be updated to that location. In other words, the used portion of the array expands
when a previously unused value is accessed, but only if it is with the arrays capacity.
Accessing any location outside of the capacity would still be an error.
Output stream (<<): Only output the used portion of the array.
Array.h code:
#ifndef ARRAY_H #define ARRAY_H
#include #include using namespace std;
template class Array {
public: Array(int arraySize= 10); // default constructor Array(const eltType[], int, int); // init from array - specify # to copy Array(const Array&); // copy constructor ~Array(); // destructor
int getNumUsed() const; // return number of elements in use int getCapacity() const; // return capacity
const Array &operator=(const Array &); // assignment bool operator==(const Array &) const; // compare if equal bool operator!=(const Array &) const; // not equal
Array &operator+=(eltType); // append element to array
eltType& operator[](int); // l-value subscript operator const eltType &operator[](int) const; // r-value subscript operator
private: void setCapacity(int); // set the capacity void setNumUsed(int); // return number of elements in use
int capacity, // capacity of the array numUsed; // number of elements in use eltType *ptr; // pointer to first element of array };
// Default constructor for class Array (default size 10) template Array::Array(int arraySize) { assert(arraySize > 0); setCapacity(arraySize); ptr = new eltType[getCapacity()]; // create space for array assert( ptr != 0 ); // terminate if memory not allocated for (int i = 0; i < getCapacity(); ++i) { ptr[i] = eltType(); } }
// constructor initializes from a standard array // designate the number of elements to copy (must be <= capacity) template Array::Array(const eltType A[], int cap, int num2copy) { assert(cap > 0 && num2copy <= cap); setCapacity(cap); ptr = new eltType[getCapacity()]; // create space for array assert( ptr != 0 ); // terminate if memory not allocated for (int i = 0; i < num2copy; ++i) { ptr[ i ] = A[i]; } for (int i = num2copy; i < getCapacity(); ++i) { ptr[i] = eltType(); } }
// Copy constructor for class Array // must receive a reference to prevent infinite recursion template Array::Array(const Array &A) { setCapacity(A.getCapacity()); ptr = new eltType[getCapacity()]; // create space for array assert( ptr != 0 ); // terminate if memory not allocated
for ( int i = 0; i < A.getCapacity(); i++ ) ptr[ i ] = A.ptr[ i ]; // copy init into object }
// Destructor for class Array template Array::~Array() { delete [] ptr; // reclaim space for array }
// Set the Array's size template void Array::setCapacity(int num) { capacity=num; }
// Get the size of the allocated array template int Array::getCapacity() const { return capacity; }
// Set the number of elements in use template void Array::setNumUsed(int num) { numUsed= num; }
// Get the number of elements in use template int Array::getNumUsed() const { return numUsed; }
// Overloaded assignment operator // const return avoids: ( a1 = a2 ) = a3 template const Array &Array::operator=( const Array &right ) {
if ( &right != this ) { // check for self-assignment // for arrays of different sizes, deallocate original // left side array, then allocate new left side array. if ( getCapacity() != right.getCapacity() ) { delete [] ptr; // reclaim space setCapacity(right.getCapacity()); // resize this object ptr = new eltType[getCapacity()]; // create space for array copy assert( ptr != 0 ); // terminate if not allocated }
for ( int i = 0; i < right.getCapacity(); i++ ) ptr[ i ] = right[ i ]; // copy array into object }
return *this; // enables x = y = z; }
// Determine if two arrays are equal and // return true, otherwise return false. template bool Array::operator==(const Array &right) const { if ( getCapacity() != right.getCapacity() ) return false; // arrays of different sizes
for ( int i = 0; i < getCapacity(); i++ ) if ( ptr[ i ] != right[ i ] ) return false; // arrays are not equal
return true; // arrays are equal }
// Determine if two arrays are not equal and // return true, otherwise return false (uses operator==). template bool Array::operator!=(const Array &right) const { return ! ( *this == right ); }
// Append element to array template Array &Array::operator+=(eltType elt) { eltType *temp=new eltType[capacity+1]; for (int i = 0; i < capacity; ++i) temp[i]=ptr[i]; temp[capacity++]=elt; delete[] ptr; ptr=temp; return(*this); }
// Overloaded subscript operator for non-const Arrays // reference return creates an lvalue template eltType &Array::operator[](int subscript) { // check for subscript out of range error assert( subscript >= 0 && subscript < getCapacity() );
return ptr[subscript]; // reference return }
// Overloaded subscript operator for const Arrays // const reference return creates an rvalue // subscript must be in range of "used" elements template const eltType &Array::operator[](int subscript) const { // check for subscript out of range error assert( subscript >= 0 && subscript < getCapacity() );
return ptr[ subscript ]; // const reference return }
// Overloaded output operator for class Array template ostream &operator<<(ostream &output, const Array& A) { int i; for ( i = 0; i < A.getCapacity(); i++ ) { output << setw( 12 ) << A[ i ]<< ",";
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output output << endl; }
if ( i % 4 != 0 ) output << endl;
return output; // enables cout << x << y; }
#endif
itest.cpp Code:
#include #include #include #include "Array.h"
using namespace std;
template void printArray(const Array &MyArray);
int main() { // create an empty Array object of capacity 4 Array iList1(4);
/////////////////////////////////// // output empty object cout << "Default Constructor" << endl; printArray(iList1);
// Append 23 and then 46 to the object // test += on object that is not at capacity cout << "Add items (under capacity)" << endl; iList1+=23; iList1+=46; // Output the Array. It should print 23, 46 printArray(iList1);
// change an existing value cout << "Change existing value" << endl; iList1[1]=50; // Output the Array. It should print 23, 50. printArray(iList1);
// add a value at the end of the unused portion cout << "Add a value at the end" << endl; iList1[3]=17; // Output the Array. It should print 23, 50, 0, 17 // number of used values is updated printArray(iList1);
// Array is now full, test += on the full object cout << "Add a value to full Array" << endl; iList1+=42; // Output the Array. It should print 23, 50, 0, 17, 42 printArray(iList1);
// Uncomment this line, and this program should crash, // as index 5 is out of range of the capacity //iList1[5]=74;
// new object using with array initialization cout << "constructor from standard array:" << endl; int array[]= { 10, 20, 30, 40 , 50 }; Array iList2(array, 10, 5); // print it, it should print 5 values printArray(iList2);
/////////////////////////////////////////////////// // // CREATE THE FOLLOWING TESTS!!!! // // test copy constructor cout << "copy constructor:" << endl;
// USE COPY CONSTRUCTOR TO CREATE iList3
// print it, it should print 5 values // printArray(iList3);
// test assignment operator cout << "assignment:" << endl;
// USE ASSIGNMENT OPERATOR TO CREATE iList4
// print it, it should print 5 values //printArray(iList4);
// CREATE THE FOLLOWING COMPARISON TESTS // test if iList1 and iList2 are equal // test if iList1 and iList2 are NOT equal // test if iList2 and iList3 are equal // test if iList2 and iList3 are NOT equal cout << "comparison operators:" << endl;
return(0); }
template void printArray(const Array &MyArray) { cout << " Capacity: " << MyArray.getCapacity() << " Elements in Use: " << MyArray.getNumUsed() << endl; cout << MyArray << endl; cout << endl; }
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