Question
Matrices are widely used in engineering calculations. An N by M matrix consists of a two dimensional array of numbers. For example the 2 by 3 matrix P is expressed as where the particular values of the elements are denoted by p11 etc. Your first task Is to develop a bask class to encapsulate the concept of an N by M matrix. Your class must encapsulate the element values as float values and it is required that the dimensions N and M and the element values can only be changes by member functions and friends of the class. It has been decided that a matrix can only be constructed in one of two ways. The first is by copying an existing matrix and the second is by creating an N by M matrix whose elements are all initialized to zero and then, to give a matrix particular values, a user calls a member function set(int i, int j, float p) which sets the value of the element at the, j position to p. Please ensure that any appropriate checks are performed on the values I and j. Please also provide a mechanism to allow the value of the element at position i, j to be read by all who wish to. You are specifically required to demonstrate that your class can be used by the following main code. It is not yet necessary to provide any functionality not explicitly needed by this main code or detailed above.
Explanation / Answer
class matrix { private: int rows, cols; float **data; public: // Constructor - takes a number of rows & columns matrix( int r, int c ) : rows(r), cols(c) { data = new float*[rows]; for( int i=0; i