/* Matrix Commutation test */
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],g[20][20], flag=1;
printf("Enter the order of the matrix A:\n");
scanf("%d%d", &m ,&n);
printf("Enter the order of the matrix B:\n");
scanf("%d%d", &n, &p);
printf("Enter the elements of the matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
printf("\n");
}
printf("Enter the elements of the matrix B:\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
scanf("%d", &b[i][j]);
printf("\n");
}
/*matrix multiplication AB */
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
e[i][j]=0;
for(k=0;k<n;k++)
e[i][j]=e[i][j]+a[i][k]*b[k][j];
}
}
printf("Product matrix AB is \n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
printf("%d\t",e[i][j]);
printf("\n");
}
/*matrix multiplication BA */
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(m==p)
g[i][j]=0;
for(k=0;k<m;k++)
g[i][j]=g[i][j]+b[i][k]*a[k][j];
}
}
printf("Product matrix BA is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",g[i][j]);
printf("\n");
}
/* Comparing two product matrices for equality */
if (m == n && n == p)
{
printf("Matrices can be compared, \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (e[i][j] != g[i][j])
{
flag = 0;
break;
}
}
}
}
else
{
printf("Product Matrices cannot be compared for commutation property.\n");
exit(1);
}
if (flag == 1)
printf("Two product matrices are equal, So the entered matrices commute with each other.");
else
printf("But, two product matrices are not equal, So the entered matrices does not commute with each other.");
return(0);
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],g[20][20], flag=1;
printf("Enter the order of the matrix A:\n");
scanf("%d%d", &m ,&n);
printf("Enter the order of the matrix B:\n");
scanf("%d%d", &n, &p);
printf("Enter the elements of the matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
printf("\n");
}
printf("Enter the elements of the matrix B:\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
scanf("%d", &b[i][j]);
printf("\n");
}
/*matrix multiplication AB */
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
e[i][j]=0;
for(k=0;k<n;k++)
e[i][j]=e[i][j]+a[i][k]*b[k][j];
}
}
printf("Product matrix AB is \n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
printf("%d\t",e[i][j]);
printf("\n");
}
/*matrix multiplication BA */
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(m==p)
g[i][j]=0;
for(k=0;k<m;k++)
g[i][j]=g[i][j]+b[i][k]*a[k][j];
}
}
printf("Product matrix BA is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",g[i][j]);
printf("\n");
}
/* Comparing two product matrices for equality */
if (m == n && n == p)
{
printf("Matrices can be compared, \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (e[i][j] != g[i][j])
{
flag = 0;
break;
}
}
}
}
else
{
printf("Product Matrices cannot be compared for commutation property.\n");
exit(1);
}
if (flag == 1)
printf("Two product matrices are equal, So the entered matrices commute with each other.");
else
printf("But, two product matrices are not equal, So the entered matrices does not commute with each other.");
return(0);
}
Comments
Post a Comment