Your Ad Here

Saturday, July 21, 2007

Matrix Multiplication - The C++ Way

As I promised eariler I have now posted a Matrix Addition And Multiplication, this program is achived by passing an Object as an Argument..............

/* Matrix Multiplication by PASSING OBJECT AS AN ARGUMENT */

class Matrix
{
int A[10][10];
int m,n;
public:
Matrix(int a,int b)
{
m = a;
n = b;
}

void readmatrix();
void printmatrix();
void addmatrix(Matrix b);
Matrix operator * (Matrix b);
};
void Matrix::readmatrix()
{
for(int i=0;i< m;i++)
{
for(j=0;j< n;j++)
cin>>A[i][j];
}
}
}
void Matrix::printmatrix()
{
for(int i=0;i< m;i++)
{
for(int j=0;j< n;j++)
{
cout< < A[i][j]<<" ";
}
cout< < endl;
}
}
void Matrix::addmatrix(Matrix b)
{
Matrix c(m,n);
for(int i=0;i< m;i++)
{
for(int j=0;j< n;j++)
{
c.A[i][j]=A[i][j]+b.A[i][j];
}
}
cout< < "The Addition Of The Two Matrices Is:"< < endl; c.printmatrix();
}

Matrix Matrix::operator*(Matrix b)
{
Matrix c(m,m);
for(int i=0;i< m;i++)
{
for(int k=0;k< m;k++)
{
c.A[i][k]=0;
for(int j=0;j< n;j++)
{
c.A[i][k] = A[i][j] * b.A[j][k] + c.A[i][k];
}
}
}
return c;
}
void main()
{
clrscr();
cout< < "Enter Order The Two Square Matrices: " ;
int a;
cin>>a;

Matrix x(a,a);
Matrix y(a,a);

cout< < endl< < "Enter Elements In First Matrix : ";
x.readmatrix();

cout< < endl< < "Enter Elements In The Second Matrix :";
y.readmatrix();

cout< < endl< < "The First Matrix:"< < endl;
x.printmatrix();

cout< < endl< < "The Second Matrix:"< < endl;
y.printmatrix();
x.addmatrix(y);

Matrix c(a,a);
c = x * y;

cout< < "The Multiplication Of The Two Matrices Are:"< < endl;
c.printmatrix();
getch();
}

/****** OUTPUT *******
Enter Order The Two Square Matrices: 3

Enter Elements In First Matrix : 1 0 0 0 1 0 0 0 1

Enter Elements In The Second Matrix :1 0 0 0 1 0 0 0 1

The First Matrix:
1 0 0
0 1 0
0 0 1

The Second Matrix:
1 0 0
0 1 0
0 0 1
The Addition Of The Two Matrices Is:
2 0 0
0 2 0
0 0 2
The Multiplication Of The Two Matrices Are:
1 0 0
0 1 0
0 0 1
*/

6 comments:

Abhishek said...

hi lionel
i saw ur program, u have declared as [10][10] bounds. this is my problem, i wish the user should input the bounds of the array
how is that done ?

Lionel said...

in that case you can ask user to enter the size
cin>>m>>n;
Matrix A(m,n)

Sania said...

Hiiii
I need matrix multiplication program but using simple arrays means not complex gunctions and classes

Anonymous said...

#include
#include

using namespace std;
const int ROWS = 3, COLS = 3;

class matrix
{
public:
matrix(int _arr[ROWS][COLS]);
matrix operator+(const matrix& m);
matrix operator*(const matrix& m);
int GetValue(const int i , const int j) const { return arr[i][j]; }
private :
int arr[ROWS][COLS];
};

matrix::matrix(int _arr[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++) // you can initilize the loop variables on the same line
{
for (int j = 0; j < COLS; j++)
arr[i][j] = _arr[i][j];
}
}

matrix matrix::operator*(const matrix& m)
{
int res[ROWS][COLS];
for( int i = 0; i < ROWS; i++)
{
for( int j = 0; j < COLS; j++)
res[i][j] = 0;
}
for( int i = 0; i < ROWS; i++)
{
for( int j = 0; j < COLS; j++)
{
for ( int k = 0; k < 3; k++)
{
res[i][j] += arr[i][k] * m.GetValue(k,j); // when using +=, make sure the variables are initilizes to some value
}
}
}
return matrix(res);
}

matrix matrix::operator+(const matrix& m)
{
int res[ROWS][COLS];
for( int i = 0; i < ROWS; i++)
{
for( int j = 0; j < COLS; j++)
{
res[i][j] = arr[i][j] + m.GetValue(i,j);
}
}
return matrix(res);
}

// this is just an over load for the matric class so that we can print it directly with cout
ostream &operator<< (ostream &os, const matrix &m)
{
for (int i = 0; i < ROWS; i++)
{
os << endl;
for (int j = 0; j < COLS; j++)
{
os << "\t" << m.GetValue(i,j);
}
}
return os;
}

int main ()
{
ifstream infile ( "matrix.dat" , ifstream::in );
int array[ROWS][COLS];
for( int i = 0; i < ROWS; i++)
{
for( int j = 0; j < COLS; j++)
{
infile >> array[i][j];
}
}
matrix m1(array); // load matrix 1 with the values read
for( int i = 0; i < ROWS; i++)
{
for( int j = 0; j < COLS; j++)
{
infile >> array[i][j];
}
}
matrix m2(array); // load matrix 2 with the value read

infile.close (); // not necessary.... the ifstream destructor will close files when destructed

cout << "Matrix 1 : " << m1 << endl; // there is not need to specift std:: is we say using namesapce std; in the begining
cout << "Matrix 2 : " << m2 << endl;
cout << "Product : " << (m1 * m2) << endl;
cout << "Sum : "<< (m1 + m2) << endl;
return 0;
}

Anonymous said...

Hi !
I need binary division in C. Can anybody help ??

Anonymous said...

there r many errors occuring :(

Your Ad Here