how can we add two matrices in the example given below?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nabeel Nazir
    New Member
    • Aug 2013
    • 5

    #1

    how can we add two matrices in the example given below?

    Code:
    class Matrix1
    {
    
    Protected:
    int ** data;
    int row,col;
    Public:
    
    };
    Class Matrix2 : Public Matrix
    {
    Public:
    Matrix2()
    {
    
    }
    Matrix2 (int a, int b):Matrix1(a,b)
    {
    
    }
    void resize()
    {
    
    //?????
    
    }
    
    };
    // can we do this in our driver/main file(function) i.e.
    Matrix2 m1(3,4);
    Matrix2 m2(3,4);
    Matrix m=m1+m2;
    // is this legal ? if yes, then how will it work ?
    Last edited by Rabbit; Aug 29 '13, 08:26 PM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Write the necessary operator+ and operator=.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      First of all, you have to define what "adding two matrices" means mathematically. What are the requirements? (Probably same number of rows and columns?) What is the expected result?
      Also, from a technical standpoint: How do you want to deal with invalid input? Just ignore it and return garbage? Return some kind of error? Try to add what you can, even though it's probably nonsense?

      Once you've decided on those things, you'll have to write the functions weaknessforcats described (or alternative functions I guess; you could name a function add if you prefer). To do so, I'd expect you need nested for loops, though the details depend on your decisions I mention above.

      Comment

      Working...