Implementing the Matrix class with the basic input/output functionality.

others

Description

This assignment consists of three tasks:

1. Implementing the Matrix class with the basic input/output functionality.

2. Implementing the matrix arithmetic using operator overloading.

3. Implementing Cramer’s Rule algorithm to solve systems of linear equations using the Matrix class for both two and three equations.

6.1 Task 1: Implementing the Matrix

File matrix.h contains an incomplete header of the Matrix class that you will implement for this

task. A two-dimensional dynamic array of double values is used to store the matrix data. The

dimensions of the array are stored in two member variables: rows and cols, which stand for

rows and columns, respectively.

6.1.1 Constructors

Matrix constructor takes two unsigned int parameters, representing the number of rows and

columns in the matrix. Upon receiving the dimension parameters, the constructor must allocate

a dynamic array of the given size, and fill it with zeros. Remember to implement a destructor

to deallocate the memory when the program exits. In addition to the constructor, implement

an appropriate copy constructor, and overload the assignment operator = to enable assigning

Matrix objects to one another.

6.1.2 Data input and output

To populate a matrix object with data, we will use operator overloading. Overload the stream

insertion operator >> to read matrix data from either the keyboard or a file stream. Make sure

you can use >> to input a n x n matrix stored in a text file in the following format:

2 4 3

5 2 3

7 1 0

Overload the stream extraction operator << to output a matrix to the screen or file in the following format:

2 4 3

5 2 3

7 1 0

7

Every element should occupy exactly 10 character spaces on the screen. The precision (number

of decimal points) should be set to 3. Hint: use setw() and setprecision() functions of the

iomanip library. Do not add any additional empty lines: just print the data.

To access individual elements of the Matrix, overload the parenthesis operator () to take two int

parameters such that the following is possible (note that matrix indices start from 0,0):

Matrix mat(2,3); / / create a 2 x 3 matrix

mat(0,0) = 5; / / set element 0 ,0 to 5

cout << mat(0,0) << endl; / / output element 0 ,0 to screen

Overloading () is very similar to overloading the subscript operator []. Note that operator ()

has to take two unsigned int input parameters.

The subscript operator [] will be used to extract separate rows of a Matrix. Extracted rows

have to be independent Matrix objects of 1 x n dimension. Overload [] to take one unsigned int

input parameter such that the following is possible:

Matrix mat(3,3); / / 3 x 3 matrix

Matrix matRow = mat[0]; / / set matRow equal to the f i r s t row of mat

/ / dimension of matRow must be 1 x 3

You should also implement getter functions to return the number of rows and the number of

columns in a matrix.


Related Questions in others category