Tuesday, December 27, 2011

WAJProgram to multiply two matrix elements if compatible and find transpose the matrix elements .


// AIM : program to multiply two matrix elements if compatible
//       and find transpose the matrix elements .

import java.io.*;

class  matmul
{
public static void main(String[] args) throws IOException
{
                int mat1[][]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[][]={{1,2,3},{4,5,6},{7,8,9}};
                int mat3[][],tmat[][],i,j,k;
mat3=new int[3][3];
                tmat=new int[3][3];
                //comaptibility or length test
                if(mat1.length!=mat2.length)
                {
                System.out.println("Matrix not compatible...");
                System.exit(0);
                }
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
for(k=0;k<=2;k++)
{
mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}

System.out.println("After Matrix Multiplicatin......");

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(mat3[i][j]+"\t");
}
System.out.print("\n");
}

                /* transpose of matrix */
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
                        tmat[i][j]=mat3[j][i];
}
}
                System.out.println("After Transpose..");

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
                        System.out.print(tmat[i][j]+"\t");
}
System.out.print("\n");
}

}
}




No comments:

Post a Comment