CS 107 - Lab 10

Creating an Array Class

This week in lab, we will write another class. This time for an array. The data members for the class will be The Array class should be written such that it will work for any data type (or just about any data type). To do this we will create a typedef to abstract the data type used in the array.

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.

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 lab10.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/lab10.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 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.