Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USING C++ You need to write a matrix class (Mat) that implements common matrix functionality. You need use the Mat.h and Mat.cpp files to write

USING C++

You need to write a matrix class (Mat) that implements common matrix functionality. You need use the Mat.h and Mat.cpp files to write this class. Your Mat class should meet the following requirements.

Dynamic Matrix Allocation

Mat m(10,2); // Allocates a matrix with 10 rows and 2 columns

Mat m; // creates a null matrix // this is a placeholder

 // with 0 rows and 0 columns  

Read and write from istream and ostream, respectively.

Reading from a istream

Mat m; // creates a null matrix

cin >> m; // 1st entry is number of rows (r) // 2nd entry is number of colums (c)

 // r times c entries for the matrix // // note that >> first allocates // the matrix of the right size and // then reads it in.  
 // the allocation only occurs if // m is a null matrix. // otherwise in case of a mismatch an // error is issued  

Writing to an ostream

Mat m(2, 3); ofstream f("output.mat"); f << m; // 1st entry is 2

 // 2nd entry is 3  

// 6 entries

f.close();

Copy constructor and assignment operator with error checking

Mat a(2,3); Mat b(a); // Using copy constructor Mat c = a; // Using assignment operator

// But the following should give an error  

Mat d(10,10); d = a; // Not the same size

Scalar arithmatic

Overload the following operators for matrix-scalar (double) addition/subtraction/multiplication/division: +, -, *, /, +=, -=, *= and /=.

Matrix arithmatic

Overload the following operators for matrix-matrix addition/subtraction/multiplication: +, -, *, +=, -= and *=. Dont forget to error check. E.g., for addition or subtraction the two matrices must be the same size, and for multiplication the number of columns of the first matrix should be equal to the number of rows of the second matrix.

Hint: checkout matrix operations in your favourite linear algebra text book.

Indexing

Overload [] operator to support the following functionality: Mat m(2,3);

cout << m[0][1] << endl; // prints the 0th row and 1st column entry

Fill up

Develop the following functions:

Destructor to deallocate the memory; Mat::ones() to initialize a matrix to all 1s; Mat::zeros() to initialize a matrix to all 0s; and Mat::rand() to intialize a matrix to random values between 0 and 1.

Your class should be called Mat It should be implemeted in files Mat.h and Mat.cpp.

A user will use your class as follows:

... #include "Mat.h" ... int main() { 
 Mat m(2,2); m.ones(); 

return 0; }

Compilation:

$ g++ main.cpp Mat.cpp -o mat_test 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions