Matrix:
-Multidimensional Array
-Matrix consist of the rows and columns.
Syntax-
array[row][column]
//Matrix with numbers of rows and columns
Multiplication of two matrix:
Condition: Number of column in first matrix=number of rows in second matrix
Example-
a[2,2]={2,2,2,2}
b[2,2]={1,1,1,1}
a*b={4,4,4,4}
program:
#include< iostream>
int main()
{
int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k,t=0;
cout<<"Enter rows and columns for Matrix_1"<< endl;
cin>>r1>>c2;
//input in matrix_1
cout<<"Enter "<< r1*c1<<" number in matrix_1";
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++){
cin>>a[i][j];
}
}
cout<<"Enter rows and columns for Matrix_2"<< endl;
cin>>r2>>c2;
//input in matrix_2
cout<<"Enter "<< r2*c2<<" number in matrix_2"<< endl;
for(i=0;i< r2;i++)
{
for(j=0;j< c2;j++){
cin>>b[i][j];
}
}
if(c1==r2)
{
for(i=0;i< r1;i++)
{
for(j=0;j< c2;j++)
{
t=0;
for(k=0;k< r1;k++)
{
t=t+a[i][k]*b[k][j];
}
c[i][j]=t;
}
}
cout<<"After multiplication"<< endl);
for(i=0;i< r1;i++)
{
for(j=0;j< c2;j++)
{
cout<< c[i][j];
}
cout<< endl;
}
}
else
cout<<"Multiplication of these two matrix is not possible";
}
Enter rows and columns in matrix_1
2 2
Enter 4 number in matrix_1
2 2 2 2
Enter rows and columns in matrix_2
2 2
Enter 4 number in matrix_2
1 1 1 1
After multiplication
4 4
4 4
Enter rows and columns in matrix_1
2 2
Enter 4 number in matrix_1
2 2 2 2
Enter rows and columns in matrix_2
3 3
Enter 9 number in matrix_2
1 1 1 1 1 1 1 1 1
Multiplication of these two matrix is not possible