BS e-Learning
BS e-Learning

Reputation: 21

Addition of Matrix in C++ Object Oriented Programming

The following code gives me the wrong output. actually, it is not doing sum, it actually copies the 2nd object to the M3 object instead of calculating the sum. I think I have some logical errors in + operator overloading. Does anybody have any idea or any other suggestion? it displays the output actually which is called in the copy constructor function cout<data[r][c]<<"\t";. but it did not display output when I use M3.displayData().

#include <iostream> 
#include <string.h>
using namespace std;

class Matrix{
    private:
        int noOfRows;
        int noOfColumns;
        int **data;
    public:
        Matrix(int noOfRows, int noOfColumns);
        void displayData();
        ~Matrix();
        Matrix (const Matrix &ref);
        Matrix operator + (Matrix m);
        Matrix& operator=(Matrix m) { 
        std::swap(m.noOfRows, noOfRows); 
        std::swap(m.noOfColumns, noOfColumns); 
        std::swap(m.data, data); 
        return *this; }
};

Matrix::Matrix(int inr=0, int inc=0){
    noOfRows=inr; noOfColumns=inc;
    data=new int*[noOfColumns];
    for(int i=0;i<noOfRows;i++)
        data[i]=new int[noOfColumns];
    int d;
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++){
            cout<<"Enter ...";cin>>d;
            data[r][c]=d;
        }
        cout<<endl;
    }
}

Matrix::Matrix (const Matrix &ref){
    this->data=new int*[ref.noOfColumns];
    for(int i=0;i<ref.noOfRows;i++)
        this->data[i]=new int[ref.noOfRows];
    
    for(int r=0;r<ref.noOfRows;r++){
        for(int c=0;c<ref.noOfColumns;c++){
            this->data[r][c]=ref.data[r][c];
            cout<<this->data[r][c]<<"\t";
        }
        cout<<endl;
    }
}

Matrix Matrix::operator + (Matrix m){
    Matrix ms(m.noOfRows,m.noOfColumns);
    ms=0;
    for (int i=0; i<m.noOfRows; i++) 
        for (int j=0; j<m.noOfColumns; j++){
        ms.data[i][j] = data[i][j]+m.data[i][j];
        return ms;
        } 
    }

void Matrix::displayData(){
    for(int r=0;r<noOfRows;r++){
        for(int c=0;c<noOfColumns;c++)
            cout<<data[r][c]<<"\t";
        cout<<endl;
    }
}

Matrix::~Matrix(){
    delete[] data;
}

int main(){
    Matrix M1(2,2),M2(2,2);
    cout<<"\n Matrix A="<<endl;
    M1.displayData();
    cout<<"\n Matrix B="<<endl;
    M2.displayData();
    cout<<"\n Sum of Matrix="<<endl;
    Matrix M3=M1+M2;
    M3.displayData();
    return 0;
}

Output is here:

Upvotes: 0

Views: 571

Answers (2)

Christophe
Christophe

Reputation: 73446

I'm not a matrix specialist, but I understood that matrix summation required both matrixes to be of the same size, and each elements to be summed up.

So you need to fully redefine operator+ (to avoid introducing exceptions here, I'd taken a permissive mathematical view, taking the max size of both matrixes and considering elements out of bounds to be 0):

Matrix Matrix::operator + (Matrix m){
    Matrix ms(max(noOfRows,m.noOfRows), max(noOfColumns+m.noOfColumns));
    for (int i=0; i<ms.noOfRows; i++) 
        for (int j=0; j<ms.noOfColumns; j++) 
           ms.data[i][j] = (i<noOfRows&&j<noOfColumns ? data[i][j]:0.0)
                             + (i<m.noOfRows&&j<m.noOfColumns ? m.data[i][j]:0.0);
    return ms;
}

By the way, it'll be safer to use the signature Matrix operator + (const Matrix& m) const, to avoid that a typo could accidentally change the value of the matrix, and avoid an unnecessary copy of the matrix argument.

Then, you must make Sum() a free standing function instead of a member function, if you want to call it like you do in main().

Upvotes: 1

user12002570
user12002570

Reputation: 1

The problem is that you have declared Sum as a friend function of class Matrix while defined it as a member function.

To solve the mentioned error just remove the Matrix:: qualification while defining it as shown below:

//----v-------------------------->removed `Matrix::` from here
Matrix Sum(Matrix m1,Matrix m2){
    Matrix m;
    m=m1+m2;
    return m;       
}

Also, the program may have other logical errors. You can refer to the rule of three for more information about them or ask a separate question if that doesn't help.

Upvotes: 0

Related Questions