7. Write a program to add two matrices of same copy. Create two objects of the class and each of which refers one 2D matrix. Use constructor to allocate memory dynamically and use copy constructor to allocate memory when one array object is used to initialize another.

// MATRIX ADDITION using DYNAMIC and COPY CONSTRUCTORS


#include<iostream.h>
 
class matrix
{
            int **a;
public:
            matrix()           // Dynamic Constructor
            {
                        int i,j;
                        a=new int*[3];
                        for(i=0; i<3; i++)
                                    a[i]=new int[3];
                        cout<<"Enter elements for a 3x3 matrix:\n";
                        for(i=0; i<3; i++)
                        for(j=0; j<3; j++)
                                    cin>>a[i][j];
            }
            matrix(matrix & x)     // Copy Constructor
            {
                        int i,j;
                        a=new int*[3];
                        for(i=0; i<3; i++)
                                    a[i]=new int[3];
                        for(i=0; i<3; i++)
                        for(j=0; j<3; j++)
                                    a[i][j]=x.a[i][j];
            }
            ~matrix()                    // Destructor
            {
                        int i;
                        for(i=0; i<3; i++)         delete a[i];
                        delete a;
            }
            void putmatrix();
            friend void add(matrix,matrix);
};
 
void matrix::putmatrix()
{
            int i,j;
            for(i=0; i<3; i++)
            {
                        for(j=0; j<3; j++)
                                    cout<<a[i][j]<<" ";
                        cout<<endl;
            }
}

void add(matrix m1,matrix m2)
{
            int i,j;
            for(i=0; i<3; i++)
            {
                        for(j=0; j<3; j++)
                                    cout<<m1.a[i][j]+m2.a[i][j]<<" ";
                        cout<<endl;
            }
}

int main()
{
            matrix obj1;
            matrix obj2(obj1);
            cout<<"Matrix 1 (and Matrix 2 also):\n";
            obj1.putmatrix();
            cout<<"SUM of the Matrices:\n";
            add(obj1,obj2);
}



Posted by Unknown On 01:57 7 comments

7 comments:

  1. THANK YOU, LOST MY EXAM BUT I HAD SOME KNOWLEDGE.

    ReplyDelete
  2. Thank u. It was really very helpfull..

    ReplyDelete
  3. Thank u. It was really very helpfull..

    ReplyDelete
  4. Thank you.Well it was nice post and very helpful information on AngularJS 5 Online Training

    ReplyDelete
  5. Thanks ....
    It was really very helpful

    ReplyDelete
  6. You completed some great factors there. I did a seek nearly the problem and determined re all folks will post when your blog. photocopy machine

    ReplyDelete
  7. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. photocopy machine

    ReplyDelete

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *