Microsoft Word - Project3-Array.docx CSC136 – Program 3, Array Assignment: Update the provided Array template class to handle partial usage. Supplied: Array template class: Array.h...

1 answer below »
C++ assignmentUnix


Microsoft Word - Project3-Array.docx CSC136 – Program 3, Array    Assignment: Update the provided Array template class to handle partial usage.   Supplied:  Array template class: Array.h    Initial test program: itest.cpp  Working executable examples: isoln, fsoln  Location:  /export/home/public/carelli/csc136/Projects/Project3  Deliverable:  Updated Array.h and completed itest.cpp program      Additional test program ftest.cpp      Do not compile the header file!  Do not turn in executables or any compiled files!!!  Due:  The programs MUST be turned in by the assigned date and time using the turnin  script. Late submissions will, in fact, be rejected by the script, resulting in a grade of  zero.     Overview:  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 wherein 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 numUsed‐1. Obviously, numUsed must be <= capacity.  > class definition has been  updated to reflect this change, with both capacity and numUsed defined. In addition, the “get”  methods for capacity and numUsed have been defined and implemented as has the default  constructor. Beyond that, a number of methods have been defined. They are written with  minimal functionality. Generally, just enough is written to allow the test program to compile.  numUsed is left unset.  You will need to go through each method and update it, rewriting and replacing code as needed  to implement both the desired overall functionality and partial array usage. Use the provided test  program, itest.cpp, to ensure that your changes work correctly. Compare your results to those  obtained from the compiled example, isoln. They should match.  Look at the Array examples provided in class. There are many relevant methods defined there!  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: This one is already implemented for you – nothing to do here. The number of  elements in use is set to zero. The default capacity is 3. The capacity can, optionally, be set to a  different value with an argument.  Constructor from dataType array: both the Array capacity and the number of elements to load  from the given dataType array will be given as arguments. Note that the number to use  (numUsed) must be <= to allocated array size (capacity)!  copy constructor: make sure everything is copied, including number of elements in use! ="" destructor: also completed for you. =""  ="" operators: ="" assignment operator (="): Like the copy constructor (watch out for self‐assignment) " 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 ([]): you need to write these – two possibilities. ="" 1.="" for the const version, an error should occur if a user attempts to access an unused ="" location.  ="" 2.="" for the non‐const version, any location index within the array’s 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 array’s capacity.  Accessing any location outside of the capacity would still be an error.  Pre‐increment(++): For this one, add an element with a default value to the end of the array. Be  sure to properly update the capacity and numUsed. Be sure to initialize the new element to a  valid default value.  Post‐increment(++): Behaves the same as the pre‐increment in that it adds a default value to the  end of the array. Be sure to make both operators work correctly when used in statements (as in  the test program)!  Dereference (*): Simply return the value of the first element in the stored array.  Output stream (<): this one is already completed for you.  input stream (="">>): Write this operator. It should add elements to the end of an existing Array  object.    Test Programs (Drivers):    The itest.cpp program includes a working function, called printArray(), for outputting Array’s  including the values for capacity and numUsed. There is also a working executable that illustrates  how the completed class should work. itest.cpp will compile and print out brief messages for each  test to be implemented. The tests are written.     You will need to complete the test program called ftest.cpp that will take a user‐supplied input  from the terminal and load it into an Array object that stores floats. You must define and use an  overloaded >> operator to load the item directly into the Array object. Don’t read it into a float  variable and then load them. Set the program up to read and store the value into default (empty)  Array object. An example executable called fsoln is provided to illustrate how it should work.    Final Notes:  Complete the updates one at a time and test after each change using itest.cpp. You don’t need a  makefile, just compile the program with your updated Array.h. Compare your results against the  provided isoln executable. You should get the same thing. Do them in the order they are tested in  itest.cpp.  Don’t forget to document the methods. There are some comments in Array.h – you have to add  the departmental standard documentation there.  Turn, in ONLY itest.cpp, ftest.cpp, and Array.h.  Grading Considerations:  1. Program must compile and be turned in on time. Turn in only itest.cpp, ftest.cpp and  Array.h  2. The methods must be well commented/documented.  a. Using the departmental standard for documentation.  3. Update the constructors to properly account for the number of elements in use  4. Update the assignment operation to properly account for the number of elements in use.  5. Properly implement the partially filled and at‐capacity cases for operator+=  6. Properly implement both the const and non‐const versions of the subscript operator as  described above.  7. Properly implement the pre‐ and post‐increment (++) operator2.  8. Test using itest.cpp.   9. Complete the input stream operator and write the test program (ftest.cpp).  Templates for adhering to the departmental documentation guidelines can be found in  CodeDocTemplate.txt under the Documents link on the course webpage.  #ifndef ARRAY_H #define ARRAY_H #include  #include  using namespace std; template  class Array { public:   Array(int size= 3);                // default constructor   Array(const Array&);               // copy constructor   Array(const dataType[], int, int); // construct from an dataType array   ~Array();                          // destructor   int getCapacity() const { return capacity; }   int getNumUsed() const { return numUsed; }   const Array &operator=(const Array &); // assignment   Array &operator+=(dataType); // append element to array   Array &operator++();         // preincrement - add default to front   Array operator++(int);       // postincrement - add default to front   bool operator==(const Array &) const;  // compare if equal   bool operator>(const Array &) const;   // just number used   dataType &operator[](int);             // l-value subscript operator   const dataType &operator[](int) const; // r-value subscript operator   dataType operator*() const;            // dereference operator private:   int capacity;              // capacity of the array   int numUsed;               // number of elements in use   dataType *ptr;             // pointer to first element of array }; // Default constructor for class Array template  Array::Array(int size) {   capacity= size;   numUsed= 0;   ptr = new dataType[capacity]; // create space for array   assert( ptr != 0 );    // terminate if memory not allocated   // initialize to default value   for (int i = 0; i < capacity; ++i) ptr[i]= datatype(); }=""  copy constructor for class array=""  must receive a reference to prevent infinite recursion=""> Array::Array(const Array &A) {   // an existing array will be   capacity= A.capacity;   ptr = new dataType[capacity]; // create space for array   assert( ptr != 0 );    // terminate if memory not allocated   for ( int i = 0; i < capacity; i++ )      ptr[ i ] =" A.ptr[ i ];  // copy init into object" }=""  constructor initializes from a standard array=""><= capacity)> Array::Array(const dataType A[], int cap, int num2copy) {   assert(cap > 0 && num2copy <= cap);   // replace what follows=""   capacity=" cap;"   ptr =" new dataType[capacity]; // create space for array"   assert( ptr !=" 0 );    // terminate if memory not allocated"   // initialize to default value=""   for (int i ="">< capacity; ++i) ptr[i]= datatype(); }=""  destructor for class array=""> Array::~Array() {   delete [] ptr;            // reclaim space for array } // Overloaded assignment operator 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 ( capacity != right.capacity ) {        delete [] ptr;                // reclaim space        capacity=right.capacity;      // resize this object        ptr = new dataType[capacity]; // create space for array copy        assert( ptr != 0 );           // terminate if not allocated     }     for ( int i = 0; i < capacity; i++ )        ptr[ i ] =" right[ i ];  // copy array into object"   }=""   return *this;   // enables x =" y = z;" }=""  overloaded subscript operator for non-const arrays=""  reference return creates an lvalue=""> dataType &Array::operator[](int subscript) {   // check for subscript out of range error   assert( subscript >= 0 && subscript < capacity );   return ptr[0];="" }=""  overloaded subscript operator for const arrays=""  const reference return creates an rvalue=""  subscript must be in range of "used" elements=""> const dataType &Array::operator[](int subscript) const {   // check for subscript out of range error   return ptr[0]; } // dereference operator (return first array value) template  dataType Array::operator*() const {   return dataType(); } // Append element to end of array template  Array &Array::operator+=(dataType elt) {   dataType *temp=new dataType[capacity+1];   for (int i = 0; i < capacity; ++i) temp[i]=ptr[i];   temp[capacity++]="elt;"   delete[] ptr;=""   ptr="temp;"   return(*this);="" }=""  preincrement - add default element to the front of array=""> Array &Array::operator++() {   return(*this); } // postincrement - add default element to the front of array template  Array Array::operator++(int) {   return(*this); } // Determine if two arrays are equal and // return true, otherwise return false. template  bool Array::operator==(const Array &right) const {   return true; } // Determine which is greater (just compare the number used) template  bool Array::operator>(const Array &right) const  {   return true; } // Overloaded output operator for class Array template  ostream &operator<> &A) {   int i;   output < "[ ";   for ( i ="">< a.getnumused(); i++ ) {      if (i !="">< ", "; }>< a[ i ];   }=""><>< endl;;><>< y; }=""  overloaded input operator for class array=""> istream &operator>>(istream &input, Array &A) {   return input;   // enables cin >> x >> y; } #endif #include  #include  #include  #include "Array.h" using namespace std; template  void printArray(const Array &); int main() {   ///////////////////////////////////   // create an empty Array object   // using a default constructor   cout <>< endl;   // output empty object=""   // test ="">> : add an item using >>   cout < "add item using input stream: ";   // output final object=""   return(0);="" }=""> void printArray(const Array &MyArray) {   cout <>< myarray.getcapacity()><><>< endl;><><>< endl;>< endl; }="" itest.cpp="" below=""> #include  #include  #include "Array.h" using namespace std; template  void printArray(const Array &); int main() {   int array[]= { 1, 2, 3, 4 };   cout < boolalpha;   for boolean testing=""   ///////////////////////////////////=""   // create an empty array object=""> iList1;   cout <>< endl;   // output empty object=""   printarray(ilist1);=""   // array constructor=""><>< endl;> iList2(array, 6, 5);   printArray(iList2);   // copy constructor   cout <>< endl;> iList3(iList2);   printArray(iList3);   // test += on object that is not at capacity   cout <>< endl;   ilist1+=" 2;"   printarray(ilist1);=""   // test +=" on object that is at capacity"><>< endl;   ilist2+=" 20;"   printarray(ilist2);=""   // change the third value in ilist1 to 6=""><><>< endl;   ilist1[0]=" 6;"   // output the array.=""   printarray(ilist1);=""   // change the third value in ilist1 to 6="">< "test non-const [] operator (> numUsed)" < endl;   ilist1[2]=" 7;"   // output the array.=""   printarray(ilist1);=""   // test ++ : remove item from ilist1=""><>< endl;   printarray(++ilist2);=""   printarray(ilist2);=""   // test ++ : add empty item to ilist1=""><>< endl;   printarray(ilist2++);=""   printarray(ilist2);=""   ///////////////////////////////////////////////=""   // complete the following tests=""   ///////////////////////////////////////////////=""   // test assignment operator=""><>< endl;> iList4;   iList4= iList1;   printArray(iList4);   // test greater than   cout < "test > operator:" < endl;>< "  ilist1 > iList1: " < (ilist2 > iList1) < endl;><>< endl;   // dereference operator=""><>< endl;><><>< endl;>< endl;   return(0);="" }=""> void printArray(const Array &MyArray) {   cout <>< myarray.getcapacity()><><>< endl;><><>< endl;>< endl; }="" desired="" output:="" default="" constructor="" capacity:="" 3,="" elements="" in="" use:="" 0="" contents:="" [="" ]="" constructor="" from="" array:="" capacity:="" 6,="" elements="" in="" use:="" 4="" contents:="" [="" 1,="" 2,="" 3,="" 4="" ]="" copy="" constructor:="" capacity:="" 6,="" elements="" in="" use:="" 4="" contents:="" [="" 1,="" 2,="" 3,="" 4="" ]="" add="" an="" item="" (+="under" capacity)="" capacity:="" 3,="" elements="" in="" use:="" 1="" contents:="" [="" 2="" ]="" add="" an="" item="" (+="at" capacity)="" capacity:="" 6,="" elements="" in="" use:="" 5="" contents:="" [="" 1,="" 2,="" 3,="" 4,="" 20="" ]="" test="" non-const="" []="" operator="">< numused)="" capacity:="" 3,="" elements="" in="" use:="" 1="" contents:="" [="" 6="" ]="" test="" non-const="" []="" operator="" (=""> numUsed) Capacity: 3, Elements in Use: 3 Contents: [ 6, 0, 7 ] Add empty item using preincrement Capacity: 7, Elements in Use: 6 Contents: [ 0, 1, 2, 3, 4, 20 ] Capacity: 7, Elements in Use: 6 Contents: [ 0, 1, 2, 3, 4, 20 ] Add empty item using postincrement Capacity: 7, Elements in Use: 6 Contents: [ 0, 1, 2, 3, 4, 20 ] Capacity: 8, Elements in Use: 7 Contents: [ 0, 0, 1, 2, 3, 4, 20 ] Assignment: Capacity: 3, Elements in Use: 3 Contents: [ 6, 0, 7 ] Test > operator: iList1 > iList1: true Test dereference operator *iList1: 6 What fsoln output Default Constructor Capacity: 3, Elements in Use: 0 Contents: [ ] Add item using input stream:
Answered Same DayApr 13, 2021

Answer To: Microsoft Word - Project3-Array.docx CSC136 – Program 3, Array...

Arun Shankar answered on Apr 14 2021
135 Votes
Array CPP solution/Array.h
#ifndef ARRAY_H
#define ARRAY_H
#include
#include
using namespace std;
template
class Array
{
public:
Array(int size= 3); // default constructor
Array(const Array&); // copy constructor
Array(co
nst dataType[], int, int); // construct from an dataType array
~Array(); // destructor
int getCapacity() const { return capacity; }
int getNumUsed() const { return numUsed; }
const Array &operator=(const Array &); // assignment
Array &operator+=(dataType); // append element to array
Array &operator++(); // preincrement - add default to front
Array operator++(int); // postincrement - add default to front
bool operator==(const Array &) const; // compare if equal
bool operator>(const Array &) const; // just number used
dataType &operator[](int); // l-value subscript operator
const dataType &operator[](int) const; // r-value subscript operator
dataType operator*() const; // dereference operator
private:
int capacity; // capacity of the array
int numUsed; // number of elements in use
dataType *ptr; // pointer to first element of array
};
// Default constructor for class Array
template
Array::Array(int size)
{
capacity= size;
numUsed= 0;
ptr=new dataType[capacity]; // create space for array
assert(ptr!=0); // terminate if memory not allocated
// initialize to default value
for (int i=0;i}
// Copy constructor for class Array
// must receive a reference to prevent infinite recursion
template
Array::Array(const Array &A)
{
// an existing array will be
capacity= A.capacity;
numUsed = A.numUsed;
ptr=new dataType[capacity]; // create space for array
assert(ptr!=0); // terminate if memory not allocated
for(int i=0;i ptr[i]=A.ptr[i]; // copy init into object
}
// constructor initializes from a standard array
// designate the number of elements to copy (must be <= capacity)
template
Array::Array(const dataType A[], int cap, int num2copy)
{
assert(cap>0 && num2copy<=cap);
// replace what follows
capacity=cap;
numUsed = num2copy;
ptr=new dataType[capacity]; // create space for array
assert(ptr!=0); // terminate if memory not allocated
// initialize to default value
for(int i=0;i ptr[i]= dataType();
for(int i=0;i ptr[i] = A[i];
}
// Destructor for class Array
template
Array::~Array() {
delete [] ptr; // reclaim space for array
}
// Overloaded assignment operator
template
const Array &Array::operator=(const Array &right)
{
if(&right!=this)
{
// check for self-assignment
//...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here