CS 107 - Lab 11

Creating a Dynamic Array Class

This week in lab, we will build on the lab written for lab 10. The difference is that we will have a dynamic array instead of a static array. We will keep the array type to the same typedef used from lab 10: typedef double arrElem The data members for the class will be

A pointer to the array is declared by putting an asterisk, *, between the data type and the identifier name, like:

     dataType * identifierName;
This array pointer will be used to store the result of the C++ new operator that will create the dynamical array.

The new operator can be used to create one or more instances of a data type. This operator is used as follows. The example shows how to create this with the double data type.

    double * dblPtr;         // a pointer to double is created

    dblPtr = new double;     // dynamically creates one instance of type double

    dblPtr = new double[10]; // dynamically creates an array of 10 instance
                             //  of type double
To create a dynamic single instance of type double doesn't make much sense, since it requires more memory to implement that a static single instance of type double, the code to access the dynamic single instance is hard to read (and understand), and you are not really gaining any advantages in doing this. The only time that creating a dynamic single instance of a data type makes sense is when you are creating a linked-list data structure (for those of you that will go on to CS 201, you will see how this is done at that time).

The creation a dynamic multiple instance of type double double creates the dynamic array, which is very useful. You would treat identifier holding the result of new operation as a regular array. This allows an array to be created when the program is running to hold the right amount of values (not too many where you waste memory and not too few where you can't store all of your values in the array).

A dynamically created array should be deallocated when the array is no longer needed. This deallocation returns this memory to the operating system to be used at some other location. Failure to return this memory can result is computer running very slowly. To deallocate this memory we use the delete operator. The delete operator will be used in one of two ways depending on how the dynamic memory was created using the new operator.

     // The following form is used when a single instance of the 
     //  data type was created.  I.E. dblPtr = new double;
     delete dblPtrl;

     // The following form is used when multiple instances of the
     //  date type were created.  I.E. dblPtr = new double[10];
     delete [] dblPtr;
When deallocating multiple instances you do not need to specify how many instances were created, the computer will take care of that automatically.

One operation that is nice with a dynamic array is to have the array grow. This is done when you already have some values stored in a dynamic array and you realize you need more space than you have already allocated. For example, let us assume we have a dynamic array of type double stored in an identifier called arrPtr and that this array holds 10 values. We have another identifier that stores this number (we will assume it is called max).

To grow the array we perform the following steps.

  1. Allocated to a temp array pointer a new dynamic array of the new size of the bigger array. Assume we want to make the new array of size 20 instead of size 10.
         double * tempArrPtr;
         tempArrPtr = new double [20];
    
  2. Copy the values from the existing dynamic array to the new dynamic array.
         for (i = 0; i < max; i++)
             tempArrPtr[i] = arrPtr[i];
    
  3. Deallocate the existing array.
         delete [] arrPtr;
    
  4. Set the original identifier to refer to the new dynamic array and update the size of the array.
         arrPtr = tempArrPtr;
         max = 20;
    

Lab Assignment

Due: By your Lab Time during the twelfth week of the semester (the week of 4/7/2003).

You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab. You are to hand in a print out of your program to do the following to your TA.

The following is the code in the file lab11.cpp. The file is also available from the ~troy directory on icarus. To copy the file from the ~troy directory on icarus to your current directory on icarus type "cp ~troy/lab11.cpp .". The two data files referenced (lab10a.data and lab10b.data) are also available from the ~troy directory on icarus.
// 	Name:
//	Email:	
//	Lab Day:
//	Lab Time:
//	TA:
//
//  Lab 11 

#include <iostream>
#include <iomanip>

using namespace std;

// The typedef for the Array class
typedef double arrElem;


// put your Array class here


int main()
{
 Array ar1, ar2, ar3(2);
 arrElem value;
 int pos, newPos;
 int i;

 // store the data from file lab10a.data into ar1
 ar1.readFromFile("lab10a.data");

 // print out the data in ar1
 cout << "Array ar1 contains " << ar1.getSize() << " values:" << endl;
 ar1.printArray();

 // Sort array ar1
 ar1.sort();

 // print the sorted array 
 cout << "Sorted array ar1 contains " << ar1.getSize() << " values:" << endl;
 ar1.printArray();

 // store the same data into ar2 and ar3
 ar2.readFromFile("lab10b.data");
 ar3.readFromFile("lab10b.data");

 // print out the data in ar2
 cout << "Array ar2 contains " << ar2.getSize() << " values:" << endl;
 ar2.printArray();

 // Sort array ar3
 ar3.sort();

 // print the sorted array 
 cout << "Sorted array ar3 contains " << ar3.getSize() << " values:" << endl;
 ar3.printArray();

 // find where the value are now located in the sorted array
 cout << "     Value     Position in ar2     Position in ar3" << endl;
 cout << "     -----     ---------------     ---------------" << endl;
 for (i = 0; i < 10; i++)
    {
     pos = i * 2;
     if (pos < ar2.getSize())
        {
         value = ar2.getValue (pos);
         newPos = ar3.find(value);
         cout << setw(10) << value 
              << setw(20) << pos 
              << setw(20) << newPos << endl;
        }
     }

}

Hand in a print out of your program to your TA.