Astro 503 Homework #4 Spring 2006 Due Feb 7, 2006 1. Write a class called Matrix3x3 which embodies the group of 3x3 real-valued matrices. Your class should provide all the methods and overloaded operators to compile the following driver routine: int main() { Matrix3x3 a; a(1,1) = 5.; a(1,2) = 7.; // ... fill all the elements this way... a(3,3) = -3.; Matrix 3x3 b; b(1,1) = 3.2; // .... fill it up... { Matrix 3x3 c = -(a*b); cout << "Element c(2,2)= " << c(2,2) << endl; c /= a; b = c; } cout << "Result for b: " << b << endl; } Note that I'm overloading the 2d element access onto the () operator. And the expression c/=a should mean c becomes inverse(a) * c. You are at liberty to choose any internal representation of the matrix as you wish, all that matters is the view of the client code. Notice that this client expects indices {1,2,3}, not zero-indexed. The default constructor should set the matrix to zero, and the copy and assignment operators should make a fresh copy of the matrix. 2. Your assignment is to write a class that implements the copy-on-write scheme described in Eckel and in lecture. Remember that this means that a copy or assignment of the class shares the data with the original, but a fresh copy is made if a class that shares data needs to change it. This class will be called Array2d, representing a 2d array of doubles (i.e. an image). Here's what the public part of the class definition should include: class Array2d { public: Array2d(const string& filename); Array2d(const Array2d& rhs); Array2d& operator=(const Array2d& rhs); void getSize(int& xsize, int& ysize) const; double& operator()(int col, int row); const double& operator()(int col, int row) const; // Fill the image with a scalar value: Array2d& operator=(double scalar); // Arithmetic operations - all element-wise const Array2d operator+(const Array2d& rhs) const; const Array2d operator*(const Array2d& rhs) const; Array2d& operator+=(const Array2d& rhs); const Array2d operator-() const; // Save to a file void writeToFile(const string& filename) const; }; Here's what the constructor will look like; we haven't done file I/O yet so I show you how to read the data from a file or write it back to one. #include using namespace std; Array2d::Array2d(const string& filename) { ifstream ifs(filename.c_str()); if (!ifs) { cerr << "could not open array file " << filename << endl; exit(1); } int xsize, ysize; ifs >> xsize >> ysize; // Here you should allocate the storage you need for an array of // size xsize x ysize. double v; int x,y; while (ifs >> x >> y >> v) (*this)(x,y) = v; } And here's the output routine: void Array2d::writeToFile(const string& filename) const { ofstream ofs(filename.c_str()); if (!ofs) { cerr << "could not open array file " << filename << endl; exit(1); } int xs, ys; getSize(xs,ys); ofs << xs << " " << ys << endl; for (int iy=0; iy