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 doubleTo 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.
double * tempArrPtr; tempArrPtr = new double [20];
for (i = 0; i < max; i++) tempArrPtr[i] = arrPtr[i];
delete [] arrPtr;
arrPtr = tempArrPtr; max = 20;
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.
This method is the default constructor. It will take no parameters. This constructor should allocate a dynamic array of size 10.
This method is to be another constructor that takes one integer value that will specify the size of the initial allocation of the dynamic array. The constructor should allocate a dynamic array of the size specified by the parameter.
This method is to take one parameter of type character array and store contents of the associated data file into the array. This function will need to open the datafile whose name is given in the parameter and read the data until the end of file is encoutered. If the file fails to open, print an error message. If the file contains more values than the dynamic array can currently hold, grow the dynamic array. The new size of the dynamic array should be twice the size of the current dynamic array. Be sure to update the data member containing the maximum number of values that could be stored in the array when the array is grown. Also, print a message when ever the array grows that states the new size of the array. This method is to set the data member containing the number of values actually stored in the array to the number of values read from the file and stored in the array. This method is not to return any value.
This method is to take no parameters and is not to return any value. This function is to print the values stored in the array in some readable format. It should print 5 values per line.
This method is to have no parameters and it to return the value of the data member which holds the number of values actually stored in the array.
This method is to have one integer parameter and it to return the value in the array at the position given in the parameter. If the parameter is larger than the number of values actually stored in the array, print an error message. The return values is to be of type arrElem that was defined in the typedef statement.
This method is to have one parameter of type arrElem as defined in the typedef statement. This method is to search the array for the value given by the parameter and return its position in the array or -1 if the value given by the parameter is not in the array. Refer to page 199 of the text for code how this is done with a function. The return value is to be of type integer.
This method is to have no parameters and is not to return a value. This method is to sort the array in ascending order. Refer to page 202 of the text for code how this is done with functions.
// 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.