Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the matrix.h file #include #include using namespace std; // Definitions of user defined type exceptions struct invalid_input : public exception { virtual const

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Here is the matrix.h file

#include

#include

using namespace std;

// Definitions of user defined type exceptions

struct invalid_input : public exception {

virtual const char* what() const throw()

{ return "Invalid matrix input"; }

};

struct incompatible_matrices : public exception {

virtual const char* what() const throw()

{ return "Incompatible matrices"; }

};

class My_matrix {

//member variables

int n, m;

int **ptr;

void allocate_memory(); // optional

public:

//member functions

My_matrix(); // default constructor

My_matrix(int n1, int m1); // parameter constructor

~My_matrix(); // destructor

My_matrix(const My_matrix& mat); // copy constructor

My_matrix(My_matrix&& mat); // move constructor (optional)

My_matrix& operator=(const My_matrix& mat); //copy assignment operator

My_matrix& operator=(My_matrix&& mat); // move assignment operator (optional)

int number_of_rows() const;

int number_of_columns() const;

int* operator()(int i) const; // overloaded to access to ith row

int& operator()(int i, int j); // overloaded to access (i,j) element

int operator()(int i, int j) const; // overloaded to access (i,j) element

int elem(int i, int j) const; // overloaded to access (i,j) element

int& elem(int i, int j); // overloaded to access (i,j) element

};

// see the handout for the description of these operators

ostream& operator

istream& operator>>(istream& in, My_matrix& mat);

My_matrix operator+(const My_matrix& mat1, const My_matrix& mat2);

My_matrix operator*(const My_matrix& mat1, const My_matrix& mat2);

//////

Here is the matrix.cpp

/*

Implementation of the clas My_matrix

*/

#include "My_matrix.h"

#include

My_matrix::My_matrix()

{

// add your code here

}

void My_matrix::allocate_memory()

{

// add your code here

}

My_matrix::My_matrix(int n1, int m1)

{

// add your code here

}

My_matrix::My_matrix(const My_matrix& mat)

{

// add your code here

}

// move constructor (optional)

My_matrix::My_matrix(My_matrix&& mat)

{

// add your code here

}

My_matrix::~My_matrix()

{

// add your code here

}

My_matrix& My_matrix::operator=(const My_matrix& mat)

{

// add your code here

}

// move assignment operator (optional)

My_matrix& My_matrix::operator=(My_matrix&& mat)

{

// add your code here

}

int My_matrix::number_of_rows() const

{

// add your code here

}

int My_matrix::number_of_columns() const

{

// add your code here

}

int* My_matrix::operator()(int i) const

{

// add your code here

}

int& My_matrix::operator()(int i, int j) const

{

// add your code here

}

int& My_matrix::operator()(int i, int j)

{

// add your code here

}

int& My_matrix::elem(int i, int j) const

{

if (i = n) throw out_of_range("Out of range");

if (j = m) throw out_of_range("Out of range");

// add your code here

}

int& My_matrix::elem(int i, int j)

{

// add your code here

}

ostream& operator

{

// add your code here

}

istream& operator>>(istream& in, My_matrix& mat)

{

// add your code here

}

My_matrix operator+(const My_matrix& mat1, const My_matrix& mat2)

{

// add your code here

}

My_matrix operator*(const My_matrix& mat1, const My_matrix& mat2)

{

// add your code here

}

/////

Here is the main.cpp

// main.cpp

// Tests all functionality of the class My_matrix class.

#include

#include

#include "My_matrix.h"

int main(){

// Some test cases are expected to throw an exception.

// Add more try and catch blocks as necessary for your code

// to finish execution.

try{

// Test 1

// * Create an object of type My_matrix, called m1, using constructor

// * Initialize m1 in main (by assigning numbers to matrix elements)

// * Display m1 on the screen using the operator

// Test 2

// * Create an object of type My_matrix, called m2, using (default)

// constructor

// * Open an input file containing a matrix (row by row)

// * Initialize m2 by reading from the input file using

// the operator >>

// * Open an output file (can be empty)

// * Write m2 to the output file using the operator

// Test 3

// * Use the copy constructor to make a copy of m1 called m3

// * Apply the copy assignment to m1 called m4

// * Display m3 and m4 on the screen using the operator

// Test 4

// * Test the matrix multiplication operator (operator*)

// * Apply the multiplication operator to valid and invalid cases

// * Display the resulting matrix and its number of rows and columns

// Test 5

// * Test the matrix addition operator (operator+)

// * Apply the multiplication operator to valid and invalid cases

// * Display the resulting matrix and its number of rows and columns

} catch(exception &error){

cerr

}

}

- operator> input operator) reads put, ow by row, to a My_matrix object. The first line should have only two numbers; the number rows and olumns. The remaining lines contain matrix rows. If the input fornat is nt correct your progrn should th the user-detined exveption incorrect input Read about exceptioas in Progrumming Principles and Pracfices using C++, pp. 1138-1139. Also, read the Seetion 24 (pp. 93-) in the course textbook - operator (output operator) prints the content of an My_matrix object, row by row, aud each row s displayed in a separate line. overloading the addition (+) ad prodet ) operators for matrices. Both the operators work on My-matrix objects. That is. c-Ate s equivalent to cli. I j-AL1] LJl+ il 11, and C=A+B s equivalent C [] [i]-.i [1] [k] *B [k] [1], 8ee the Background section of this assignment for more details. Notice that the number of rows mnst be the same ss the number of columns for both the matrix arguments for the aiditioi operatu. If this condition is not satistied an thrown. Aleo, the mumber of colu of the ist argument must be the same as the number ol rows ol the second argment for the produet operator. If this condition is not satisfied exception shoald be thrunn. exceptioa should be Implementation and Testing 1. Download te supplementary tar file with a sampk oode from the class wehpage. 2. Your fies should be arranged as follows aDeclaation o My matrix class in Hy natrix.h (b) Definition (implementation) of My_matzix class in My matrix,cpp (Reading from a file, writing to a file and testing My matrix operations in main.cpp (d) Makefile is caled by make 3. ( points) Implement, compile, run and test Phase 1 of the assignment Compile your program using the following Linux machine coamand line ake all And then un your program by exeenting main 4. (2 pt) Implement, ompile, un and test Phase 2 of the assignment: A generic version of My matrix (a) The templated class My_matrix ses data type as a paraeter Recall the teanplated vector material, And ten ruun your peogram by executing 4 20 pt) Implement, compile, run and test Phase 2 of the 8 signment: A generic version of My mal ILK (a) The templated class Mymatrix data type as a parameter ecall the templated vector material. slides 16-22 and follow the instructions below i. Templates should be declared and defined in the TemplatodMy natrix.h file. Move the content of My_matrix.cpp and My_-matrix.h to TemplatedMy matrix.h ii. Replace int type by eeie type T. Later, in the main function, T conld be specified as any mmerie type: deuble, float, long, r poibly a ser defined type. ii. To cresate a tesuplated class with seneric type I, you mst replace declaration andfar return type iv. Use the geueric type T anywhee thoghout the elass TemplatedMy_natrix. v. Add the keywrd template stypename T> before a class declaration. vi. If a member funetlon s definod outside the class deelaratlion, change the funetlon signature by replacing My_matrix:: with TenplatcdMy_matrix<: compile and ru the generie version similarly as in part of tbe asaignment test all operatious for ge vesion using at least three ditferent types objects. points prepare a report l.yx pae lyx electronie version. point progran description parpose assiguve desxription data structures instructions to coanpile run your program including input output specifications any logieal exceptions possible bug descriptions l c chjert oriented cr peneric programming tetnres inellxling fiatures. evideces testng operations bonus d theut stl class vector instead two dimensional array r provide overlo dol my nat type hjerte file edit selection view go debug terminal help matrix.h-cade-visaal studio codr g my. matrix.cpp .matrch x open editors include> G My matrix.cpp X C My-matrix,h 9 frinclude 16 CODE 11 using namespace std; 12 I Detinitions ot user def ined type exceptions 13 struct invalid_input public exception ( C Jmain.cpp E . Makefile C. My matrix.cpp 14 virtual const char* what() const thro) return "Invalid matrix input; 16 main.cpp M Makefile C My matrixucpp C My matrich 17 18 struct incompatible matrices : public exception t 2 19 vrtual const char what () const thro) 20 return "Incompatible matrices" 21 23 class My matrix ( 24 25 26 int n, n; 27 int *ptr; 28 void allocate_nemory) optional 29 ao public: 31 2 My matrix) default constructor 33 Ny matrix(int nl, int nl); par ameter constructor 31 y matrix( / destructor 35 Ny matrix(const My matrix mat); copy constructor 36 Ny matrix(My matrix&& mat) move constructor (optional) 7 My matrix operator (const My matrix& mat); //copy assipnment operator 3 Ny matrix& operator-( 39 int number_of_rows() const; 40 int number of columns) const 41 | nt* operator() (int i) const; // overloaded to access to ith row 42 int& operator()(int i, int j); overloaded to access (i,j) element 41 | nt operator()(int , int j) const; // overloaded to access (1.]) element 44 int elen(int i, int j) const; overloaded to access (i,j) element as int& elem(int i, int j); over loxled to access (i,j) elenent member variahles member funclions (My matrix&& mat) move assignnent operator (optional) OUTLINE d File Edit Selection View Go Debug Terminal Help My matrix.h-Cade-Visaal Studio Codr C My matrich x 1 struct incompatible matrices puhlic exception ( OPEN EDITORS 2 19 virlual const chart w) cnst Uhrow) G My matrix.cpp X C My-matrix,h 2(return "Incompatible matrices") 21 CODE class My_natrix t C Jmain.cpp E. Makrfile C. My matrix.cpp 23 24 25 /member variables int n, n; main.cpp 27 | int **ptr; M Makefile void allocate memory); optional 29 3 public: 31 32 Ny matrix); / de-fault constructor 3] Hy matrix(int n1, ntn1); // parameter constructor 34 My matrix); destructor 35 Ny matrix(const My matrix mat); / copy constructor 36 Ny matrix(My matrix&& mat)move constructor (optional) 3 Ny matrix& operator-(const My_natrix mat); copy assignment operator 38 My matrix& operalor-(My matrix& mat) move assiginent operator (uptional) 9 int numher of rows( const; 0 init number of_columns() consl; A1 | nt* operator()( int ) cnnst; // overinaded to access to ith row 42 43 44 int elen(int i, int i) const; overloaded to access (i,j) element 45 46 My-matrix-cpp C My matrich /mcmber functionis int& operator) (int i, int j); i/ overloaded to access (i,i) element int operator) (int i, int j) const; overloaded to access (i,j) elenent int& elem(int i, int j); overloaded to access (i,j) element 48 see the handout for the description of these operators as ostream operator>(istream& in, Ny matrix& mat); 51 My matrix operator+(const Hy matrix& mat1, const Ny matrix& mat?); 52 My matrix opr(const My matrix& mati, const My matrix& mat2); 5R 54 OUTLINE d File Edit Selection View Go Debug Terminal Help My matrix.cp Code Visial Stuxdia Code c- My-matrix.cpp My.matric.h M Mokefile OPEN EDITORS 1UNSAVED #include "My matrix"h" My matrix.cpp C My matrix.h M Makefile 6 include -n) >-m) throw out of-range("out of throw out-of-range("Out of range"); range"); OUTLINE 80 d File Edit Selection View Go Debug Terminal Help My matrix.cp Code Visial Stuxdia Code My matrix.cppC My matrixh M Mokefile OPEN EDITORS 1UNSAVED 72 / add your code here My matrix.cpp C My matrix.h M Makefile 71 75 int& My-matrix: ; elem(int i, int j) const 76 77 if (i-m) Lhrow out-of range( "Out uf range"); 79 CODE . main.cpp . Maketile My matrix.cpp main.cpp add your code here 82 int& My matrix::elem(int i, int j) M Makefile 84 35 86 87 add your code here & My matrix.cpp C My matrix.h ostream& operator input operator) reads put, ow by row, to a My_matrix object. The first line should have only two numbers; the number rows and olumns. The remaining lines contain matrix rows. If the input fornat is nt correct your progrn should th the user-detined exveption incorrect input Read about exceptioas in Progrumming Principles and Pracfices using C++, pp. 1138-1139. Also, read the Seetion 24 (pp. 93-) in the course textbook - operator (output operator) prints the content of an My_matrix object, row by row, aud each row s displayed in a separate line. overloading the addition (+) ad prodet ) operators for matrices. Both the operators work on My-matrix objects. That is. c-Ate s equivalent to cli. I j-AL1] LJl+ il 11, and C=A+B s equivalent C [] [i]-.i [1] [k] *B [k] [1], 8ee the Background section of this assignment for more details. Notice that the number of rows mnst be the same ss the number of columns for both the matrix arguments for the aiditioi operatu. If this condition is not satistied an thrown. Aleo, the mumber of colu of the ist argument must be the same as the number ol rows ol the second argment for the produet operator. If this condition is not satisfied exception shoald be thrunn. exceptioa should be Implementation and Testing 1. Download te supplementary tar file with a sampk oode from the class wehpage. 2. Your fies should be arranged as follows aDeclaation o My matrix class in Hy natrix.h (b) Definition (implementation) of My_matzix class in My matrix,cpp (Reading from a file, writing to a file and testing My matrix operations in main.cpp (d) Makefile is caled by make 3. ( points) Implement, compile, run and test Phase 1 of the assignment Compile your program using the following Linux machine coamand line ake all And then un your program by exeenting main 4. (2 pt) Implement, ompile, un and test Phase 2 of the assignment: A generic version of My matrix (a) The templated class My_matrix ses data type as a paraeter Recall the teanplated vector material, And ten ruun your peogram by executing 4 20 pt) Implement, compile, run and test Phase 2 of the 8 signment: A generic version of My mal ILK (a) The templated class Mymatrix data type as a parameter ecall the templated vector material. slides 16-22 and follow the instructions below i. Templates should be declared and defined in the TemplatodMy natrix.h file. Move the content of My_matrix.cpp and My_-matrix.h to TemplatedMy matrix.h ii. Replace int type by eeie type T. Later, in the main function, T conld be specified as any mmerie type: deuble, float, long, r poibly a ser defined type. ii. To cresate a tesuplated class with seneric type I, you mst replace declaration andfar return type iv. Use the geueric type T anywhee thoghout the elass TemplatedMy_natrix. v. Add the keywrd template stypename T> before a class declaration. vi. If a member funetlon s definod outside the class deelaratlion, change the funetlon signature by replacing My_matrix:: with TenplatcdMy_matrix<: compile and ru the generie version similarly as in part of tbe asaignment test all operatious for ge vesion using at least three ditferent types objects. points prepare a report l.yx pae lyx electronie version. point progran description parpose assiguve desxription data structures instructions to coanpile run your program including input output specifications any logieal exceptions possible bug descriptions l c chjert oriented cr peneric programming tetnres inellxling fiatures. evideces testng operations bonus d theut stl class vector instead two dimensional array r provide overlo dol my nat type hjerte file edit selection view go debug terminal help matrix.h-cade-visaal studio codr g my. matrix.cpp .matrch x open editors include> G My matrix.cpp X C My-matrix,h 9 frinclude 16 CODE 11 using namespace std; 12 I Detinitions ot user def ined type exceptions 13 struct invalid_input public exception ( C Jmain.cpp E . Makefile C. My matrix.cpp 14 virtual const char* what() const thro) return "Invalid matrix input; 16 main.cpp M Makefile C My matrixucpp C My matrich 17 18 struct incompatible matrices : public exception t 2 19 vrtual const char what () const thro) 20 return "Incompatible matrices" 21 23 class My matrix ( 24 25 26 int n, n; 27 int *ptr; 28 void allocate_nemory) optional 29 ao public: 31 2 My matrix) default constructor 33 Ny matrix(int nl, int nl); par ameter constructor 31 y matrix( / destructor 35 Ny matrix(const My matrix mat); copy constructor 36 Ny matrix(My matrix&& mat) move constructor (optional) 7 My matrix operator (const My matrix& mat); //copy assipnment operator 3 Ny matrix& operator-( 39 int number_of_rows() const; 40 int number of columns) const 41 | nt* operator() (int i) const; // overloaded to access to ith row 42 int& operator()(int i, int j); overloaded to access (i,j) element 41 | nt operator()(int , int j) const; // overloaded to access (1.]) element 44 int elen(int i, int j) const; overloaded to access (i,j) element as int& elem(int i, int j); over loxled to access (i,j) elenent member variahles member funclions (My matrix&& mat) move assignnent operator (optional) OUTLINE d File Edit Selection View Go Debug Terminal Help My matrix.h-Cade-Visaal Studio Codr C My matrich x 1 struct incompatible matrices puhlic exception ( OPEN EDITORS 2 19 virlual const chart w) cnst Uhrow) G My matrix.cpp X C My-matrix,h 2(return "Incompatible matrices") 21 CODE class My_natrix t C Jmain.cpp E. Makrfile C. My matrix.cpp 23 24 25 /member variables int n, n; main.cpp 27 | int **ptr; M Makefile void allocate memory); optional 29 3 public: 31 32 Ny matrix); / de-fault constructor 3] Hy matrix(int n1, ntn1); // parameter constructor 34 My matrix); destructor 35 Ny matrix(const My matrix mat); / copy constructor 36 Ny matrix(My matrix&& mat)move constructor (optional) 3 Ny matrix& operator-(const My_natrix mat); copy assignment operator 38 My matrix& operalor-(My matrix& mat) move assiginent operator (uptional) 9 int numher of rows( const; 0 init number of_columns() consl; A1 | nt* operator()( int ) cnnst; // overinaded to access to ith row 42 43 44 int elen(int i, int i) const; overloaded to access (i,j) element 45 46 My-matrix-cpp C My matrich /mcmber functionis int& operator) (int i, int j); i/ overloaded to access (i,i) element int operator) (int i, int j) const; overloaded to access (i,j) elenent int& elem(int i, int j); overloaded to access (i,j) element 48 see the handout for the description of these operators as ostream operator>(istream& in, Ny matrix& mat); 51 My matrix operator+(const Hy matrix& mat1, const Ny matrix& mat?); 52 My matrix opr(const My matrix& mati, const My matrix& mat2); 5R 54 OUTLINE d File Edit Selection View Go Debug Terminal Help My matrix.cp Code Visial Stuxdia Code c- My-matrix.cpp My.matric.h M Mokefile OPEN EDITORS 1UNSAVED #include "My matrix"h" My matrix.cpp C My matrix.h M Makefile 6 include -n) >-m) throw out of-range("out of throw out-of-range("Out of range"); range"); OUTLINE 80 d File Edit Selection View Go Debug Terminal Help My matrix.cp Code Visial Stuxdia Code My matrix.cppC My matrixh M Mokefile OPEN EDITORS 1UNSAVED 72 / add your code here My matrix.cpp C My matrix.h M Makefile 71 75 int& My-matrix: ; elem(int i, int j) const 76 77 if (i-m) Lhrow out-of range( "Out uf range"); 79 CODE . main.cpp . Maketile My matrix.cpp main.cpp add your code here 82 int& My matrix::elem(int i, int j) M Makefile 84 35 86 87 add your code here & My matrix.cpp C My matrix.h ostream& operator

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

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

What is the relationship between humans?

Answered: 1 week ago

Question

What is the orientation toward time?

Answered: 1 week ago