Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// ************************************************************************** // CPSC2620 Spring 2020 - Prof Zhang I can't seem to use my destructor as when I include my deallocate function, it is

// ************************************************************************** // CPSC2620 Spring 2020 - Prof Zhang

I can't seem to use my destructor as when I include my deallocate function, it is called multiple times and i lose memory. When i comment out the deallocate function in the destructor the program will run perfectly but with memory Leaks.

Any information would help.

#include"./matrixExt.h" #include #include #include

using namespace std;

//**************************************************************************** // Default constructor. Default dimensions are 2 x 2 //**************************************************************************** CMatrixExt::CMatrixExt(int d) { assert(d>=0); n = d; M = new int*[d]; // blck is an array of pointers to ints for(int i=0; i

//**************************************************************************** // copy constructor //**************************************************************************** CMatrixExt::CMatrixExt(const CMatrixExt &matrix){

M = allocateMemory(matrix.n); for(int i=0; i

cout<

//**************************************************************************** // Function to read from an input stream //**************************************************************************** void CMatrixExt::readMatrix() { for(int i = 0; i> *(*(M+i)+j); // same as M[i][j] } } //**************************************************************************** // Function to print a matrix //**************************************************************************** void CMatrixExt::printMatrix() { for(int i = 0; i

//**************************************************************************** // Functions to "return" return the dimensions of the Matrix //**************************************************************************** int CMatrixExt::getDimension() const { return n; } //**************************************************************************** // returns an element from the matrix //**************************************************************************** int CMatrixExt::getElement(int i,int j){ return M[i][j]; }

//**************************************************************************** // Functions to replace an element and returns the old value //**************************************************************************** int CMatrixExt::replaceElementAt(int i, int j, int newInt) { assert(i>0 && i <= n && j>0 && j<=n); int oldVal = *(*(M+i-1)+j-1); *(*(M+i-1)+j-1) = newInt; return oldVal; }

//**************************************************************************** // Functions to resize a matrix. //**************************************************************************** void CMatrixExt::resizeMatrix(int newSize) { assert(newSize >=1); int oldSize = n; if(!(newSize==n)) // do nothing if newSize is same as current dimension { int** temp = allocateMemory(newSize); n = newSize; if(oldSize < newSize) { for(int i=0; i

//**************************************************************************** // makes an identity matrix corresponding to n (dimension) //**************************************************************************** void CMatrixExt::makeIdentity() { for(int i=0;i

//**************************************************************************** //will copy a matrix element by element //**************************************************************************** void CMatrixExt::copyMatrix(const CMatrixExt &matrix) { assert(n=matrix.n); M = matrix.M; }

//**************************************************************************** // Add two matricies of the same size //**************************************************************************** CMatrixExt CMatrixExt::addMatrix(const CMatrixExt &matrix)const { assert(n==matrix.n); CMatrixExt temp(n);

for(int i =0;i

return temp; }

//**************************************************************************** // adds a constant value to every element //**************************************************************************** CMatrixExt CMatrixExt::addMatrix(int x) { CMatrixExt temp(n);

for(int i =0;i

return temp; }

//**************************************************************************** //Subtracts the elements of two matricies the same size //**************************************************************************** CMatrixExt CMatrixExt::subtractMatrix(const CMatrixExt &matrix)const { assert(n==matrix.n); CMatrixExt temp(n);

for(int i =0;i

return temp; }

//**************************************************************************** //Subtracts the same value from every element in matrix //**************************************************************************** CMatrixExt CMatrixExt::subtractMatrix(int x) { CMatrixExt temp(n);

for(int i =0;i

//**************************************************************************** //multiply two matrices that are compatible //**************************************************************************** CMatrixExt CMatrixExt::multiplyMatrix(const CMatrixExt &matrix) { CMatrixExt mul(3); for(int i =0;i

//**************************************************************************** //multiply matrix elements by the same value //**************************************************************************** CMatrixExt CMatrixExt::multiplyMatrix(int x) { CMatrixExt temp(n);

for(int i =0;i

//************************************************************************** // Private helper function to allocate new memory //**************************************************************************** int** CMatrixExt::allocateMemory(int d) { int** temp; temp = new int*[d]; for(int i=0; i

//**************************************************************************** // Private helper function to deallocate a memory block from the heap //****************************************************************************

void CMatrixExt::deallocateMemory(int **matrix, int d){ for(int i = 0; i < d; i++) { delete [] matrix[i]; } delete [] matrix;

matrix = nullptr; }

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

3. How old are they? (children, teens, adults, seniors)

Answered: 1 week ago

Question

4. Where do they live? (city or town, state, country)

Answered: 1 week ago