matrix process in c++

-1

For my midterm assignment I am required to code a matrix with all details (like identify any size matrix, add (etc.) a number to matrix, add (etc.) two matrices together, transpose, determinant, print...).

I made a class called Matrix and wrote bunch of constructors and functions and overloaded operators.

#include <iostream>
#include <string>
#include <ctime>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include <cmath>

using namespace std;

class Matrix {
private:
    int row;
    int column;
    int value;
    int** matrix;
public:
    Matrix(int ro, int col, int val);
    Matrix(int ro, int col, char type);
    ~Matrix();
    void print();
    void resize(int ro , int col);

    void operator=(const Matrix& other);
    Matrix operator+(int num)const;

};

Matrix::Matrix(int ro=10, int col=10, int val=0)
    :row(ro), column(col), value(val) 
    {
    if (row <= 0 || column <= 0) {
        cout << "invalid row or column value";
    }
    else{
        matrix = new int* [row];
        for (int i = 0; i < row; i++) {
            matrix[i] = new int[column];

        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = value;
            }
        }
    }
}

Matrix::Matrix(int ro, int col, char type)
    :row(ro), column(col)
    {
    if (row <= 0 || column <= 0) {
        cout << "invalid row or column value";
    }
    else {
        matrix = new int* [row];
        for (int i = 0; i < row; i++) {
            matrix[i] = new int[column];
        }

        if (type == 'e'){

            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    if (i == j) {
                        value = 1;
                        matrix[i][j] = value;
                    }
                    else {
                        value = 0;
                        matrix[i][j] = value;
                    }
                }
            }
        }
        srand(time(NULL));
        if (type == 'r') {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    value = rand() % 256;
                    matrix[i][j] =value;
                }
            }
        }
    }
}



void Matrix::operator=(const Matrix& other) {

    this->resize(other.row, other.column);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            matrix[i][j] = other.matrix[i][j]; // this line throw exception:Exception thrown at 0x00A7BCF2 in matrix.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD.
        }
    }
}

Matrix Matrix::operator+(int num) const {

    Matrix model(row, column);
    for (int i = 0; i < model.row; i++) {
        for (int j = 0; j < model.column; j++) {
            model.matrix[i][j] = matrix[i][j] + num;
        }
    }
    return model;
}

void Matrix::print() {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            cout<< setw(6) << matrix[i][j]<< " " ;
        }
        cout << endl;
    }
}

Matrix::~Matrix() {
    for (int i = 0; i < row; i++) {
        delete[] matrix[i];
    }
    delete[] matrix;
}


   void Matrix::resize(int ro, int col){
    int** copymatrix;
    copymatrix = matrix;
    matrix = new int* [ro];
    for (int i = 0; i < ro; i++) {
        matrix[i] = new int[col];
}
    for (int i = 0; i < ro; i++) {
        for (int j = 0; j < col; j++) {
            if (i >= row || j >= column) {
                matrix[i][j] = 0;
            }
            else {
                matrix[i][j] = copymatrix[i][j];
            }
        }
    }
    row = ro;
    column = col;
}

int main() {

    Matrix mat1(3, 6, 2);

    cout << endl;
    mat1 = mat1 + 5;
    mat1.print();


}

I also tried this but gives me the same error

Matrix Matrix::operator=(const Matrix& other) {

    Matrix model(other.row, other.column);
    for (int i = 0; i < model.row; i++) {
        for (int j = 0; j < model.column; j++) {
            model.matrix[i][j] = other.matrix[i][j]; //exception 
        }
    }
    return model;
}

I actually have problems with operators overloading. As I write in the main function when I use operator+ overloading it returns a matrix and after that it comes to operator= overloading and there is the problem as it returns the matrix and comes to operator= it gives me the error Exception thrown at 0x00CEBCDB in matrix.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD. I think I know why but I don't know how to solve it.

This code works in CodeBlocks 17.12. It throws the error in Visual Studio 2019 (v142).

c++
matrix
operator-overloading
asked on Stack Overflow Oct 26, 2019 by user12279525 • edited Nov 2, 2019 by JaMiT

2 Answers

0

Your assignment operator doesn't assign new memory for the matrix:

void Matrix::operator=(const Matrix& other) 
{
    this->resize(other.row, other.column);
    for (int i = 0; i < row; i++)
    for (int j = 0; j < column; j++) 
    {
        matrix[i][j] = other.matrix[i][j];
    }
}

So, after it, you'll be accessing memory through an uninitialized pointer.

You need assignment operators, constructors that correctly allocate memory for your matrices. You need matching destructors, too. Look up "rule of 3".

-1
Matrix& Matrix::operator=(const Matrix& other) {
    if (this == &other) {
        return *this;
    }
    this->resize(other.row, other.column);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            matrix[i][j] = other.matrix[i][j];
        }
    }
    return *this;
}

Matrix Matrix::operator+(int num) const {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            this->matrix[i][j] += num;
        }
    }
    return *this;
}

When Function return a matrix by finishing the scope Destructor get called and after we call assignment operator overloading on it, it can't reach the member of that matrix because it already get deleted by Destructor so it throws a exception. I wrote this before but it did not work until i realize that we should put (&) for (=) operator if we want use this operators overloading one after the other. so now it's working well

answered on Stack Overflow Oct 27, 2019 by user12279525

User contributions licensed under CC BY-SA 3.0