"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Matrix (Multiplication). Show all posts
Showing posts with label Matrix (Multiplication). Show all posts

Thursday, February 18, 2010

Product of Matrices


#include<iostream.h>
#include<conio.h>
#include<process.h>



void main()
{
clrscr();
int A[10][10] , B[10][10] , P[10][10] , i, j, k, m, n, p, q;

cout<< "\n Enter the rows & column of Matrix A :;
cin>> m>> n;

cout<< "\n Enter the rows & column of Matrix  B :;
cin>> p>> q;

if(n ! = p)
{
cout<< "\n The Matrix Multiplication is impossible .";
 getch();
exit(1);
}

cout<< " \ n  Enter the elements for Matrix A :";
for(i = 0; i< m ; i++)

for(j = 0 ; j< n ; j++)
   cin >> A[i][j];

cout<< " \ n Enter the elements for Matrix B :";

for(i = 0; i< p ; i++)
for(j = 0 ; j< q ; j++)
  cin >> B[i][j];




/* ......Product ...... */

for(i = 0; i< m ; i++)
for(j = 0 ; j< q ; j++)
{


P[i][j] = 0;


for(k = 0; k< n ; k++)


P[i][j] = P[i][j] + A[i][k] * B[k][j];


}



cout<< "\n Matrix A : \n ";
for( i =0; i< m ; i++)
{ for(j = 0; j< n ; j++)
     cout<< A[i][j];
 cout<< endl;
}


cout<< "\n Matrix B: \n ";



for( i =0; i< p ; i++)
{ for(j = 0; j< q ; j++)
     cout<< B[i][j];
  cout<< endl;
}
cout<< "\n Product AXB : \n ";
for( i =0; i< m ; i++)
  { for(j = 0; j< q ; j++)
       cout<< P[i][j];
    cout<< endl;
  }


getch();

}

Write a program to obtain the product of the following matrices:

           5    9    -2
A =      8    0    5
           0    2    8




          2  4
B =     0   1
          1  3


A x B =      8     23
                21    47
                8     26
          


#include<iostream.h>
#include<conio.h>
#include<process.h>


void main()
{
clrscr();


int A[3][3] = { 5, 9, -2,
                     8,0,5 ,
                     0,2, 8 };


int B[3][2] = { 2,4 ,
                      0 , 1,
                      1,3 } ;


int P[3][2] , i, j, k;


/*  ......Product ...... */


for(i = 0; i< 3 ; i++)
for(j = 0 ; j< 2 ; j++)
{
  P[i][j] = 0;
for(k = 0; k< n ; k++)
 P[i][j] = P[i][j] + A[i][k] * B[k][j];
}




cout<< "\n Matrix A : \n ";


for( i =0; i< 3 ; i++)
{  for(j = 0; j< 3 ; j++)
       cout<<  A[i][j];
   cout<< endl;
}


cout<< "\n Matrix B: \n ";




for( i =0; i< 3 ; i++)
{ for(j = 0; j< 2 ; j++)
     cout<<  B[i][j];
cout<< endl;
}


cout<< "\n  Product AXB  : \n ";




for( i =0; i< 3 ; i++)
{ for(j = 0; j< 2 ; j++)
   cout<< P[i][j];
cout<< endl;
}


getch();


}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.