0

I was wondering how to go about this. I have a matrix object that is a 2d array that grabs a matrix. I am trying to build an add method to add my matrices together.I am a second semester student and have more methods but i am stuck on how to go about this. we have done up to overloading operators, dynamic memory and pointers. I just need some guidance to help me learn this stuff effectively instead of trying for 12 hours. Any help would be grateful.

my class function is

Matrix Matrix:: add(const Matrix & m) const
{
    Matrix v; // I know to make a matrix to add

    // I tried this but seems to not work
    v=v[rows][cols]+ m[rows][cols];
    return v
}

I think I have figured it out now. Thanks! But i will still vote best answer so have at it! It might be useful for other students.

Cferrel
  • 321
  • 2
  • 5
  • 19
  • Of course this will not work. where are rows, cols defined? Whats the definition of your Matrix class? You will have to loop over the rows and cols and add every element. Btw. I would not use a 2d array go with a 1d instead and use [width * row + col] to index it. – P3trus Jan 24 '12 at 22:19

2 Answers2

0

You should overload operator+ and operator+= to do this.

Matrix& operator+ (Matrix lhs, const Matrix& rhs)
{
    //do your addition
    return *this;

}

For more detailed information on operator overloading, see this question thread

Community
  • 1
  • 1
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • I have to build my method due to class requirements and i have been reading and trying to find examples to work with to get an idea. – Cferrel Jan 24 '12 at 22:14
  • @JavaProgrammer you should post a bit more code in order for us to be able to help you. We have no idea what your `Matrix` class looks like. – Tony The Lion Jan 24 '12 at 22:19
0

I'm going to take a stab and assume your matrix class looks something like this:

class Matrix
{
    public:
        int width;
        int height;
        int data[width][height];
}

If you want to add two Matrixes then you need some sort of function that takes two Matrixes and produces a third matrix such that each value in data is the addition of the two corresponding values in the original matrixes. One such function might look like this:

Matrix add(const Matrix& lhs, const Matrix& rhs)
{
    Matrix output;
    for(int row = 0; row < lhs.height; ++row)
    {
        for(int column = 0; column < lhs.widtht; ++column)
        {
            output.data[row][column] = lhs.data[row][column] + rhs.data[row][column];
        }
    }
    return output;
}

We're going to assume that the matrixes are the same size and ignore the problems different sized matrixes cause. So all we do is loop through both matrixes and add the values to a new matrix. Now we can call code like:

Matrix a; // Pretend this already has data
Matrix b; // Ditto
Matrix c = add(a, b);

You can see where this is going, all that's left is to make it an operator, generally when you type

a + b

What the compiler actually see's is

operator+(a, b)

So all that's left is to rename the add function to

Matrix operator+(const Matrix& lhs, const Matrix& rhs)
Jake Woods
  • 1,808
  • 15
  • 20