Write a program for matrix multiplication in c++ And Algorithm
Alogrithm:
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
Multiplication of two matrixes:
Rule: Multiplication of two matrixes is only possible if first matrix has size m X n and other matrix has size n x r. Where m, n and r are any positive integer.
Multiplication of two matrixes is defined as
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 2 x 2 and 2 x 3 respectively:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h> // for setw()
int main()
{
int m1[10][10],m2[10][10],add[10][10],i,j,r1,c1,c2,r2,k;
clrscr();
cout<<"Enter how row na column you wants in matrix 1:";
cin>>r1>>c1;
cout<<"Enter how row na column you wants in matrix 2:";
cin>>r2>>c2;
if(r1!=c1)
cout<<"Enter 1st matrix's row and 2nd matrix's column same:";
cout<<"Enter for first matrix:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>m1[i][j];
}
}
cout<<"Enter for sencond matrix:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>m2[i][j];
}
}
cout<<"The multiplication two of matrix is:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<endl;
add[i][j]=0;
for(k=0;k<r1;k++)
{
add[i][j]+=m1[i][k]*m2[k][j];
}
}
cout<<setw(10)<<add[i][j];
}
/* cout<<"The addition of matrix is:\n";
for(i=0;i<c1;i++)
{
cout<<endl;
for(j=0;j<r1;j++)
{
cout<<setw(5)<<add[i][j];
}
}*/
getch();
return 0;
}
0 comments:
Post a Comment