CS 311 Fall 2009  >  Assignment 2

CS 311 Fall 2009
Assignment 2

Assignment 2 is due at 5 p.m. Thursday, September 24. It is worth 25 points.

Procedures

This assignment is to be done individually.

E-mail answers to the exercises below to ffggc@uaf.edu, using the subjectDA2”.

Exercises (25 pts total)

Exercise A — “Kinda-Smart Array” Class

Purpose

In this exercise, you will write a simple class that manages an array. It will not be quite as “smart” as it could be (std::vector is much smarter), but it should be better than a built-in C++ array (for example, it knows its size, and has copy operations).

Key to this assignment is proper management of a resource in a class. Be sure you have no resource leaks, and ownership and related issues are properly documented.

And as before, make your code high quality.

Instructions

This assignment is to be done individually.

Implement a C++ class template that manages and allows access to a fixed-size array. The type of item in the array and the number of items in the array should be specified by the client. Be sure to follow the coding standards. Standard 3A (“Requirements on template parameter types must be documented.”) now applies. You do not need to follow standards 3B, 3C, or 3D.

Example Code

Here is some code using a KSArray. Some of it will not compile, as noted.

KSArray<int> ia(10);          // array of 10 ints
KSArray<int> iax;             // another array of 10 ints
KSArray<double> da(40, 3.2);  // array of 40 doubles, each of which is set to 3.2
KSArray x(10);                // WILL NOT COMPILE; no template parameter

// Set all items (counter loop)
for (int c = 0; c < ia.size(); ++c)
{
    ia[c] = c * c;
}

// Print all items (iterator loop)
const int * iter;
for (iter = ia.begin(); iter != ia.end(); ++iter)
{
    cout << "Item :" << *iter << endl;
}

const KSArray<int> ia2(ia);   // Copy constructor
if (ia2 == ia)                // Condition should be true
    cout << "Equal!" << endl;

KSArray<double> da2;
da2 = da;                     // Copy assignment
da2 = ia;                     // WILL NOT COMPILE; different types

if (da == ia)                 // WILL NOT COMPILE; different types
    cout << "ZZZ" << endl;

Test Program

I have written a test program: ksarray_test.cpp. If you compile and run your package with this program (unmodified!), then it will test whether your package works properly.

Do not turn in ksarray_test.cpp.


CS 311 Fall 2009: Assignment 2 / Updated: 21 Sep 2009 / Glenn G. Chappell / ffggc@uaf.edu Valid HTML 4.01!