Wednesday, October 19, 2011

Program: Multiplication of Matrices




Multiplication of 3x3 matrices

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>

void main()
{
clrscr();
int x[20][20], y[20][20], z[20][20], m, n, p, q, k, r, c;
cout<<"Enter size of matrix X (for row and column) :";
cin>>m>>n;
cout<<"\n\nEnter size of matrix Y (for row and column) ;";
cin>>p>>q;
if (n!=p)
{
cout<<"\n\nMultiplication is not possible.";
getch();
exit(0);
}
else
{
for (r=0; r<m; r++)
{
for (c=0; c<n; c++)
{
cout<<"Enter value of matrix X : ";
cin>>x[r][c];
}
}
for (r=0; r<p; r++)
{
for (c=0; c<q; c++)
{
cout<<"Enter value for matrix Y : ";
cin>>y[r][c];
}
}
for (r=0; r<m; r++)
{
for (c=0; c<q; c++)
{
z[r][c] = 0;
for (k=0; k<n; k++)
{
z[r][c] = z[r][c] + x[r][k]*y[k][c];
}}}

clrscr();
cout<<"Matrix X\n--------\n\n";

for (r=0; r<m; r++)
{

for (c=0; c<n; c++)
{
cout<<setw(5)<<x[r][c];
}

cout<<"\n";}
cout<<"\n\nMatrix Y\n--------\n\n";
for (r=0; r<m; r++)
{

for (c=0; c<n; c++)
{
cout<<setw(5)<<y[r][c];
}
cout<<"\n";
}
cout<<"\n\nMatrix Z\n--------\n";

for (r=0; r<m; r++)
{
for (c=0; c<n; c++)
{
cout<<setw(5)<<z[r][c];
}
cout<<"\n";
}

getch();
}

Article by
Chief-Editor

He is a tech Enthusiast, an Altruist and calm mentor. He loves to code. He is also a creative graphic designer. Do check out his designfolio @ Jasmeet.asia If you like This post, you can follow me on Twitter.



For Further Reading,
C++ Program, Control Structures

0 comments:

Post a Comment