Question: It is a c++ assignment. i need somone to make short the code line in Matrix_test.cpp file //Matrix.h #include using namespace std; class Matrix {
It is a c++ assignment. i need somone to make short the code line in Matrix_test.cpp file
//Matrix.h
#include
//Matrix.cpp
#include"Matrix.h"
Matrix::Matrix() { matrix = new float*[0]; matrix[0] = new float[0]; }
Matrix::~Matrix() { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) delete matrix[i]; } delete[] matrix; }
Matrix::Matrix(int r, int c) { rows = r; cols = c; matrix = new float*[rows]; for (int i = 0; i < cols; i++) matrix[0] = new float[i]; }
float *Matrix::setSize(int x, int y) { float **m= NULL ; if (x > rows && y > cols) { //allocate new matrix m = new float*[x]; for(int i = 0; i < y; i++) m[0] = new float[i]; } //now copy elements of to new if (m!=NULL) for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { m[i][j] = matrix[i][j]; } } if (m != NULL) return &m[0][0]; return NULL; } float *Matrix::getMatrix() { return &matrix[0][0]; } float *Matrix::getRow(int r) { return &matrix[r][0]; } float *Matrix::getColumn(int c) { return &matrix[0][c]; }
//Martrix_test.cpp
#include"Matrix.h"
int main() { Matrix m(3,4); cout << "Address of the first position M[0,0] in the Matrix : " << m.getMatrix() << endl; cout << "Calculate the address of the last position : " << &m.matrix[2][3] << endl; cout << "Calculate the address of an entry inside the matrix M[3,4]: " << &m.matrix[3][4] << endl;; cout << "Calculate the address of an entry outside the lower bound: " << &m.matrix[-1][-1] << endl; cout << "Calculate the address of an entry outside the lower bound: " << &m.matrix[4+1][4+1] << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
