Значения извлекаемые из двумерного массива не соответствуют ожиданиям

Итак, суть проблемы. После создания матрицы (любого размера) и попытки получить любой её элемент, к примеру с координатами (0;0), возвращается какой-то мусор, но не значение элемента матрицы.

Вот тут создаю нулевую матрицу размером 3 на 3. При этом в данном фрагменте переменной d присваивается то значение, которое и должно быть (0)

void __fastcall TMainFrame::FormCreate(TObject *Sender){
    mArray[0] = new Matrix(3,3);
    double d = (*mArray[0])[0][0];
    MatrixTabControlChange(Sender);
}

А вот тут уже переменной d присваивается какой-то мусор. Данный метод вызывается из FormCreate.

void __fastcall TMainFrame::MatrixTabControlChange(TObject *Sender){
    //...
    double d = (*mArray[0])[0][0];
    //...
}

Класс Matrix

class Matrix {
public:
    //...
    Matrix(int width, int height);
    //...
    double *operator[] (const int index);
    //...
private:
    double **matrix;
    //...
};

и реализация

Matrix::Matrix(int width, int height) : columns(width), rows(height){
    matrix = new double*[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new double[columns];
        for (int j = 0; j < columns; ++j) {
            matrix[i][j] = 0;
        }
    }

}

double *Matrix::operator[](const int index) {
    return matrix[index];
}

Буду рад, если кто-нибудь сможет помочь мне разобраться с этой проблемой. Пишу на Turbo C++ 2006.

Ниже привёл весь код Variable.h

#ifndef KURSOVAYA_VARIABLE_HPP
#define KURSOVAYA_VARIABLE_HPP


#include "exception"

class Matrix {
public:
    Matrix(double **matrix, int width, int height);

    Matrix(int width, int height);

    Matrix();

    Matrix(const Matrix &rhs);

    virtual ~Matrix();

    bool operator==(const Matrix &rhs) const;

    bool operator!=(const Matrix &rhs) const;

    Matrix operator+(const Matrix &rhs) const;

    Matrix operator+(const double num) const;

    Matrix operator-(const Matrix &rhs) const;

    Matrix operator-(const double num) const;

    Matrix operator*(const double num) const;

    Matrix operator*(const Matrix &rhs) const;

    Matrix transposition() const;

    double determinant() const;

    Matrix minor() const;

    Matrix algebraicComplement() const;

    double *operator[] (const int index);

    void setColumns(int columns);

    int getColumns() const;

    void setRows(int rows);

    int getRows() const;

private:
    double **matrix;
    int columns, rows;
    void deleteMatrix();
};

class IncorrectMatrixSizeException : std::exception{
public:
    IncorrectMatrixSizeException() : std::exception(){

    }
};


#endif //KURSOVAYA_VARIABLE_HPP

Variable.cpp

#include "Variable.h"

Matrix::Matrix(double **matrix, int width, int height) : matrix(matrix), columns(width), rows(height) {}

Matrix::Matrix(int width, int height) : columns(width), rows(height){
    matrix = new double*[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new double[columns];
        for (int j = 0; j < columns; ++j) {
            matrix[i][j] = 0;
        }
    }

}

Matrix::Matrix(){
    matrix = new double*[1];
    matrix[0] = new double[1];
    matrix[0][0] = 0;
}

Matrix::Matrix(const Matrix &rhs) {
    columns = rhs.columns;
    rows = rhs.rows;
    matrix = new double*[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new double[columns];
        for (int j = 0; j < columns; ++j) {
            matrix[i][j] = rhs.matrix[i][j];
        }
    }
}

Matrix::~Matrix() {
    deleteMatrix();
}

bool Matrix::operator==(const Matrix &rhs) const {
    if (columns != rhs.columns ||
        rows != rhs.rows) return false;
    for (int i = 0; i < rows; ++i)
        for (int j = 0; j < columns; ++j) {
            if (matrix[i][j] != rhs.matrix[i][j]) return false;
        }
    return true;
}

bool Matrix::operator!=(const Matrix &rhs) const {
    return !(rhs == *this);
}

Matrix Matrix::operator+(const Matrix &rhs) const {
    if (columns != rhs.columns || rows != rhs.columns) throw IncorrectMatrixSizeException();
    Matrix result = Matrix(rhs);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            result.matrix[i][j] += matrix[i][j];
        }
    }
    return result;
}

Matrix Matrix::operator+(const double num) const {
    Matrix result = Matrix(*this);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            result.matrix[i][j] += num;
        }
    }
    return result;
}

Matrix Matrix::operator-(const Matrix &rhs) const {
    if (columns != rhs.columns || rows != rhs.columns) throw IncorrectMatrixSizeException();
    Matrix result = Matrix(rhs);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            result.matrix[i][j] -= matrix[i][j];
        }
    }
    return result;
}

Matrix Matrix::operator-(const double num) const {
    Matrix result = Matrix(*this);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            result.matrix[i][j] -= num;
        }
    }
    return result;
}

Matrix Matrix::operator*(const double num) const {
    Matrix result = Matrix(*this);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            result.matrix[i][j] *= num;
        }
    }
    return result;
}

Matrix Matrix::operator*(const Matrix &rhs) const {
    if (columns != rhs.rows) throw IncorrectMatrixSizeException();
    double **c = new double*[rows];
    for (int i = 0; i < rows; ++i) {
        c[i] = new double [rhs.columns];
        for(int j = 0; j < rhs.columns; j++) {
            c[i][j] = 0;
            for(int k = 0; k < columns; k++)
                c[i][j] += matrix[i][k] * rhs.matrix[k][j];
        }
    }
    return Matrix(c, columns, rhs.rows);
}

Matrix Matrix::transposition() const {
    double **c = new double*[columns];
    for (int i = 0; i < columns; ++i) {
        c[i] = new double[rows];
        for (int j = 0; j < rows; ++j)
            c[i][j] = matrix[j][i];
    }
    return Matrix(c,rows, columns);
}

// Получение матрицы без i-й строки и j-го столбца
void getMatr(double **mas, double **p, int i, int j, int m) {
    int di, dj;
    di = 0;
    for (int ki = 0; ki< m - 1; ki++) { // проверка индекса строки
        if (ki == i) di = 1;
        dj = 0;
        for (int kj = 0; kj<m - 1; kj++) { // проверка индекса столбца
            if (kj == j) dj = 1;
            p[ki][kj] = mas[ki + di][kj + dj];
        }
    }
}

double determinant(double** matrix, int size){
    int i, j, d, k, n;
    double **p;
    p = new double*[size];
    for (i = 0; i<size; i++)
        p[i] = new double[size];
    j = 0; d = 0;
    k = 1; //(-1) в степени i
    n = size - 1;
    if (size<1) throw IncorrectMatrixSizeException();
    if (size == 1) {
        d = matrix[0][0];
        return(d);
    }
    if (size == 2) {
        d = matrix[0][0] * matrix[1][1] - (matrix[1][0] * matrix[0][1]);
        return(d);
    }
    if (size>2) {
        for (i = 0; i<size; i++) {
            getMatr(matrix, p, i, 0, size);
            d = d + k * matrix[i][0] * ::determinant(p, n);
            k = -k;
        }
    }
    return(d);
}

double Matrix::determinant() const {
    if (rows != columns) throw IncorrectMatrixSizeException();
    return ::determinant(matrix, rows);
}

Matrix Matrix::minor() const {
    //todo
    return Matrix(NULL, 0, 0);
}

Matrix Matrix::algebraicComplement() const {
    //todo
    return Matrix(NULL, 0, 0);
}

double *Matrix::operator[](const int index) {
    return matrix[index];
}

void Matrix::setColumns(int columns){
    double **matrix = new double*[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new double[columns];
        for (int j = 0; j < columns; ++j) {
            if (this->columns > columns) {
                matrix[i][j] = j < this->columns ? this->matrix[i][j] : 0;
            }
        }
    }
    deleteMatrix();
    this->columns = columns;
    this->matrix = matrix;
}

int Matrix::getColumns() const{
    return columns;
}

void Matrix::setRows(int rows){
    double **matrix = new double*[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new double[columns];
        for (int j = 0; j < columns; ++j) {
            if (this->columns > columns) {
                matrix[i][j] = i < this->rows ? this->matrix[i][j] : 0;
            }
        }
    }
    deleteMatrix();
    this->rows = rows;
    this->matrix = matrix;
}

int Matrix::getRows() const{
    return rows;
}

void Matrix::deleteMatrix(){
    for (int i = 0; i < rows; ++i) {
        delete [] matrix[i];
    }
    delete [] matrix;
}

MainFrame.h

//---------------------------------------------------------------------------

#ifndef MainFraimH
#define MainFraimH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Grids.hpp>
#include <ComCtrls.hpp>
#include "Command.h"
//---------------------------------------------------------------------------
#define VAR_MATRIX_COUNT 26
#define ARRAY_SIZE VAR_MATRIX_COUNT + 1
//---------------------------------------------------------------------------
class TMainFrame : public TForm
{
__published:    // IDE-managed Components
    TGroupBox *MathOperationGroupBox;
    TButton *PlusButton;
    TButton *MinusButton;
    TButton *MultiplyButton;
    TButton *DivideButton;
    TButton *DegreeButton;
    TButton *TransponentButton;
    TButton *DeterminantButton;
    TButton *BracketsButton;
    TButton *Button1;
    TButton *ClearButton;
    TEdit *Edit1;
    TTabControl *MatrixTabControl;
    TEdit *EditRowCount;
    TLabel *Label1;
    TEdit *EditColumnCount;
    TLabel *Label2;
    TStringGrid *MatrixStringGrid;
    TButton *AddButton;
    TButton *DeleteButton;
    void __fastcall MatrixInputButtonClick(TObject *Sender);
    void __fastcall ClearButtonClick(TObject *Sender);
    void __fastcall AddButtonClick(TObject *Sender);
    void __fastcall DeleteButtonClick(TObject *Sender);
    void __fastcall EditRowCountChange(TObject *Sender);
    void __fastcall EditColumnCountChange(TObject *Sender);
    void __fastcall MatrixTabControlChange(TObject *Sender);
    void __fastcall FormCreate(TObject *Sender);
private:    // User declarations
    void clear();
    Matrix *mArray[ARRAY_SIZE];
public:     // User declarations
    __fastcall TMainFrame(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TMainFrame *MainFrame;
//---------------------------------------------------------------------------
#endif

MainFrame.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainFraim.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainFrame *MainFrame;
//---------------------------------------------------------------------------
__fastcall TMainFrame::TMainFrame(TComponent* Owner)
    : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TMainFrame::MatrixInputButtonClick(TObject *Sender)
{
    //MatrixInputEdit1
}
//---------------------------------------------------------------------------

void __fastcall TMainFrame::ClearButtonClick(TObject *Sender)
{
    clear();
}
//---------------------------------------------------------------------------

void TMainFrame::clear(){

}
//---------------------------------------------------------------------------

void __fastcall TMainFrame::AddButtonClick(TObject *Sender)
{
    if(MatrixTabControl->Tabs->Count == VAR_MATRIX_COUNT)
        return;
    for(int i = 1; i < VAR_MATRIX_COUNT; i++){
        if(mArray[i] == NULL){
            char c = i + 'A';
            MatrixTabControl->Tabs->Add(c);
            mArray[i] = new Matrix(1,1);
            return;
        }
    }
}
//---------------------------------------------------------------------------


void __fastcall TMainFrame::DeleteButtonClick(TObject *Sender)
{
    int i = MatrixTabControl->TabIndex;
    if(i == 0)
        return;
    MatrixTabControl->Tabs->Delete(i);
    delete mArray[i];
    mArray[i] = NULL;
    MatrixTabControl->TabIndex = MatrixTabControl->Tabs->Count - 1;
}
//---------------------------------------------------------------------------


void __fastcall TMainFrame::EditRowCountChange(TObject *Sender)
{
    int i = MatrixTabControl->TabIndex;
    mArray[i]->setRows(EditRowCount->Text.ToInt());
    MatrixTabControlChange(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TMainFrame::EditColumnCountChange(TObject *Sender)
{
    int i = MatrixTabControl->TabIndex;
    mArray[i]->setColumns(EditColumnCount->Text.ToInt());
    MatrixTabControlChange(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TMainFrame::MatrixTabControlChange(TObject *Sender)
{
    Matrix *matrix = mArray[MatrixTabControl->TabIndex];

    MatrixStringGrid->RowCount = matrix->getRows();
    MatrixStringGrid->ColCount = matrix->getColumns();

    EditRowCount->Text = IntToStr(matrix->getRows());
    EditColumnCount->Text = IntToStr(matrix->getColumns());

    for(int row = 0; row < matrix->getRows(); row++)
        for(int column = 0; column < matrix->getColumns(); column++){
            double d = (*mArray[0])[0][0];
            MatrixStringGrid->Cells[row][column] = FloatToStr((*matrix)[row][column]);
        }
}
//---------------------------------------------------------------------------

void __fastcall TMainFrame::FormCreate(TObject *Sender)
{
    mArray[0] = new Matrix(3,3);
    double d = (*mArray[0])[0][0];
    MatrixTabControlChange(Sender);
}
//---------------------------------------------------------------------------

Ответы (0 шт):