Friday, January 2, 2015

C++ Program for multiplying two matrices


#include<iostream>
#include<iomanip>
using namespace std;

const int ROWS=3;
const int COLS=3;

int r,c;

void get_value(int [][COLS]);

int main()
{
const int N=3;
int mat1[ROWS][COLS];
int mat2[ROWS][COLS];
int mulMat[ROWS][COLS]={0};
cout << "Enter the elements of mat 1:\n";
get_value(mat1);
cout << "Enter the elements of mat 2:\n";
get_value(mat2);

for( r=0 ; r<ROWS ; r++ )
{
for( c=0 ; c<COLS ; c++ )
{
for( int i=0 ; i<N ; i++ )
{
mulMat[r][c]+=mat1[r][i]*mat2[i][c];
}
}
}
cout << "The Product mat is:\n";
for( r=0 ; r<ROWS ; r++ )
{
for( c=0 ; c<COLS ; c++ )
{
cout << setw(5)
<< mulMat[r][c];
}
cout << endl;
}

cout << endl;

return 0;
}

void get_value(int values[][COLS] )
{
for( r=0 ; r<ROWS ; r++ )
{
for( c=0 ; c<COLS ; c++ )
{
cout << "Enter the element "
<< (r+1) << "x" << (c+1)
<< ": ";
cin >> values[r][c];
}
}
}

No comments:

Post a Comment