Creating your own type definitions is discussed on page 419 of the book (not on page 414 as the index says). The syntax for this is:
typedef known_type_definition new_type_name;A known_type_definition includes the basic data types (int, char, float, double, bool, etc), plus the names of declared classes and structures, plus any previously created new_type_names. The new_type_name must follow the rules for an identifier (letter or underscore first, followed by letters, digits or underscores). Once a new type is created, this type can be used to create variables. An example for this could be:
typedef double speed; speed sp1, sp2;The variables of sp1 and sp2 are used just like regular doubles, but this use of a typedef makes the code more readable. For this lab, you are to use the typedef of
typedef double arrElem;and create your Array class to have an array of arrElems. The purpose here is that once we get the Array class working, we would only have to change the known_type_definition in the typedef to create a working Array class for some other type instead of an array of doubles.
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 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 can be stored in the array, print an error message. 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 10 #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; 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.