rootUser

Matrix multiplication

May 26th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Java programme to multiply mattrix*/
  2. package Matrix;
  3. import java.util.Scanner;
  4. public class Matrix1
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.        
  9.         int r1, c1, r2, c2, i=0, j=0, k;
  10.         int[][] a=new int[10][10];
  11.         int[][] b=new int[10][10];
  12.         Scanner myobject = new Scanner(System.in);
  13.         System.out.println("Enter rows and column for first matrix: ");
  14.         r1=myobject.nextInt();
  15.         c1=myobject.nextInt();
  16.         System.out.println("Enter rows and column for second matrix: ");
  17.         r2=myobject.nextInt();
  18.         c2=myobject.nextInt();
  19.         int[][] mult=new int[r1][c2];
  20.         /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
  21.         while (c1!=r2)
  22.         {
  23.             System.out.println("Error! column of first matrix not equal to row of second.\n\n");
  24.             System.out.println("Enter rows and column for first matrix: ");
  25.             r1=myobject.nextInt();
  26.             c1=myobject.nextInt();
  27.             System.out.println("Enter rows and column for second matrix: ");
  28.             r2=myobject.nextInt();
  29.             c2=myobject.nextInt();
  30.         }
  31.         /* Storing elements of first matrix. */
  32.         System.out.println("Enter elements of matrix 1:");
  33.         for(i=0; i<r1; ++i)
  34.         {
  35.             for(j=0; j<c1; ++j)
  36.             {
  37.                 System.out.println("Enter elements"+" a["+(i+1)+"]["+(j+1)+"]: ");
  38.                 a[i][j]=myobject.nextInt();
  39.             }
  40.         }
  41.  
  42.         /* Storing elements of second matrix. */
  43.         System.out.println("\nEnter elements of matrix 2 : ");
  44.         for(i=0; i<r2; ++i)
  45.         {
  46.             for(j=0; j<c2; ++j)
  47.             {
  48.                 System.out.println("Enter elements"+" b["+(i+1)+"]["+(j+1)+"]: ");
  49.                 b[i][j]=myobject.nextInt();
  50.             }
  51.         }
  52.         /*Initializing elements of matrix mult to 0.*/
  53.         for(i=0; i<r1; ++i)
  54.         {
  55.             for(j=0; j<c2; ++j)
  56.             {
  57.                 mult[i][j]=0;
  58.             }
  59.         }
  60.  
  61.         /* Multiplying matrix a and b and storing in array mult. */
  62.         for(i=0; i<r1; ++i)
  63.         {
  64.             for(j=0; j<c2; ++j)
  65.             {
  66.                 for(k=0; k<c1; ++k)
  67.                 {
  68.                     mult[i][j]+=a[i][k]*b[k][j];
  69.                 }
  70.             }
  71.         }
  72.         /* Displaying the multiplication of two matrix. */
  73.         System.out.println("\nOutput Matrix:\n");
  74.         for(i=0; i<r1; ++i)
  75.         {
  76.             for(j=0; j<c2; ++j)
  77.             {
  78.                 System.out.println("  "+mult[i][j]);
  79.                 if(j==c2-1)
  80.                 {
  81.                     System.out.println("\n\n");
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
Add Comment
Please, Sign In to add comment