Thursday 6 August 2015

Java program for multiplication of original matrix and transpose matrix


Input: s = 1
Matrix elements increased by one.
Original Matrix:

1  2  3 
4  5  6
7  8  9

Transpose Matrix:

1  4  7
2  5  8
3  6  9

Multiplication of two matrices:

14  32  50
32  77  122
50  122  194

Program:

import java.io.*;
import java.util.*;
public class matmul
{
          public static void main( String[ ] args ) {
          Scanner n = new Scanner( System.in );
          int s, i, j, k;
          int a[ ][ ] = new int[ 10 ] [ 10 ];
          int b[ ][ ] = new int[ 10 ][ 10 ];
          int mul[ ][ ] = new int[ 10 ][ 10 ];
          System.out.println( "Enter S value: " );
          s = n.nextInt( );
          for( i = 0 ; i < 3 ; i++)
         {
                 for( j = 0 ; j < 3 ; j++ )
                {
                        a[ i ][ j ] = s;
                        s = s + 1;
                 }
          }
          System.out.println( "Original Matrix: " );
          for( i = 0 ; i < 3 ; i++ )
          {
                  for( j = 0 ; j < 3 ; j++ )
                 {
                        System.out.print( a[ i ][ j ] + "  " );
                  }
                  System.out.println("");
           }
           for( i = 0 ; i < 3 ; i++ )
          {
                   for( j = 0 ; j < 3 ; j++ )
                  {
                         b[ i ][ j ] = a[ j ][ i ];
                   }
           }
           System.out.println( "Traspose Matrix:" );
           for( i = 0 ; i < 3 ; i++)
           {
                   for( j = 0 ; j < 3 ; j++)
                  {
                          System.out.print( b[ i ][ j ] + "  " );
                   }
                   System.out.println( "" );
            }
            for(i=0 ; i < 3 ; i++ )
           {
                    for( j = 0 ; j < 3 ; j++)
                   {
                           for( k = 0 ; k < 3 ; k++ )
                          {
                                  mul[ i ] [ j ] = mul[ i ] [ j ] + ( a[ i ] [ k ] * b[ k ] [ j ] );
                           }
                    }
             }
             System.out.println( "Multiplication of two matrices: " );
             for( i = 0 ; i < 3 ; i++ )
            {
                    for( j = 0 ; j < 3 ; j++ )
                   {
                            System.out.print( mul[i][j] + "  " );
                    }
                    System.out.println( " " );
             }
       }
}



0 comments:

Post a Comment