Wednesday, 30 May 2012


DOWNLOAD PROGRAM FILES FOR TWO D MATRIX OPEARTION.CPP HERE

OTHER DOWNLOADS:

File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction

VISIT SITE LIFEQUEST.COM



Hi friends,
Below example illustrate how can we program for two d matrix multiplication,addition,substraction.

Let's first see how are two d matrix are stored in memory:

two d matrix is we can it array of array, that is array within array. In two dimensional matrix array elements strored in consecutive memory blocks for exp. ifg array 2*2 then first array is stored with it,s two column then secon array withit's two column and so on.

Look at below: 3*3 matrix


[0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]

Keep in mind that in memory block array row and column numbering start from 0 therefore above 3*3 matrix has row's from 0 to 2 and  columns from 0 to 2.

Now let look at how addition,subtraction and multiplication operation carried out.

Addition operation in tow d matrix:

a00 a01 a02   A00 A01 A02   a00+A00 a01+A11 so on..
a10 a11 a12 + A10 A11 A12 = a10+A10 a11+A11
a20 a21 a22   A20 A21 A22   a20+A20 a21+A20

Matrix subtraction:


a00 a01 a02   A00 A01 A02   a00-A00 a01-A11 so on..
a10 a11 a12 - A10 A11 A12 = a10-A10 a11-A11
a20 a21 a22   A20 A21 A22   a20-A20 a21-A20



Matrix multipliclation:


a00 a01 a02   A00 A01 A02   a00xA00+a01xA10+a02xA20 so on
a10 a11 a12 x A10 A11 A12 = a10xA00+a11xA10+a12xA20 
a20 a21 a22   A20 A21 A22   a20xA00+a21xA10+a22xA20


So now you are familiar with how matrix operation computed:

let's look at code how this operation is carried out:
DOWNLOAD PROGRAM FILES FOR TWO D MATRIX OPEARTION.CPP HERE


Matrix definition:


class matrix
{
   public:
   int a[3][3];


   matrix()
   {}
   void getdata()
   {
    cout<<"Enter any number:"<<endl;
     for(int i=0;i<3;i++)
      {
       for(int j=0;j<3;j++)
{
cin>>a[i][j];
}
cout<<endl;
      }
   }


   


};

i have overloaded operator +,-,* for matrix operations, for better understanding of this look at links:

Operator overloading concept in c++

Matrix Addition:


   matrix operator+(matrix m)

   {
      matrix m5;
      for(int i=0;i<3;i++)
      {
for(int j=0;j<3;j++)
{
m5.a[i][j]= a[i][j] + m.a[i][j];
}
      }
return m5;
    }

Matrix subtraction:

   matrix operator-(matrix m1,matrix m2)
   {
      matrix m6;
      for(int i=0;i<3;i++)
      {
for(int j=0;j<3;j++)
{
m6.a[i][j]= m1.a[i][j] - m2.a[i][j];
}
      }
return m6;
   }

Matrix multiplication:



    matrix operator*(matrix m1,matrix m2)
   {
      matrix m6;
      for(int i=0;i<3;i++)
      {
for(int j=0;j<3;j++)
{
 m6.a[i][j] = 0;


 for(int k=0;k<3;k++)
 {
   m6.a[i][j]+= m1.a[i][k] * m2.a[k][j];
 }
}
      }
return m6;
   }



Matrix display function:


void display()
   {
      for(int i=0;i<3;i++)
      {
for(int j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
      }
   }


 So that is it all necessary functions download program from below :
DOWNLOAD PROGRAM FILES FOR TWO D MATRIX OPEARTION.CPP HERE

Output snap:
Hope you have enjoy it!

See other programs:

File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction


VISIT SITE LIFEQUEST.COM








No comments:

Post a Comment